Search code examples
swaggeropenapi

Does OpenAPI support regex in path, like ExpressJS does?


For languages with diacritics it would be very useful to allow regex in paths, for example for the Portuguese word código, allowing both codigo and código, example:

  /c(o|ó)digo_postal/{cp}:
    get:
      operationId: getPostalcodeInfo
      summary: Detalhes sobre Código Postal (CP4 ou CP7)
      parameters:
        - in: path
          name: cp
          required: true
          schema:
            type: string
            pattern: /^\d{4}(\p{Dash}?\d{3})?$/u
          examples:
            cp4:
              value: '2495'
              summary: Código Postal CP4
            cp7:
              value: '2495-300'
              summary: Código Postal CP7
          description: Código Postal
        - $ref: '#/components/parameters/json'
      responses:
        '200':
          description: Detalhes sobre Código Postal CP4 ou CP7
          content:
            application/json:
              schema:
                oneOf:
                - $ref: '#/components/schemas/CP4'
                - $ref: '#/components/schemas/CP7'
        '404':
          description: Código Postal não encontrado
          content:
            application/json:
              schema:
                type: string

But it is failing on my validator. I read the specs but they don't mention anything.


Solution

  • OpenAPI does not support regex in paths.

    It does support references though, so you could have both paths be a simple $ref entry to the same Path Item, effectively allowing what you're suggesting here.

    I think the other alternative is simply to use a sanitised string (I think I'd be tempted to do that for paths anyway), but as an English-only speaker, I don't know if that makes sense in the context or not.