Forum Discussion

gary002g's avatar
gary002g
New Contributor
4 years ago

Defining conditional attributes in OpenAPI

I need to define a request for a searching service in JSON. Request can have either geographical coordinates (longitude and latitude) or postal code. Either one must be present. If longitude is pre...
  • HKosova's avatar
    HKosova
    4 years ago

    (Copying my answer from Stack Overflow)

     

    To define oneOf/anyOf logic for request parameters, you would have to define all parameters as a single object-type parameter, as explained here.

     

    parameters:
      - in: query
        name: params  # This name will NOT appear in the request URL
                      # but will be used in generated client SDKs / server stubs
        required: true
    
        # serialize this object as ?key1=value1&key2=value2
        style: form
        explode: true
    
        schema:
          type: object
          properties:
            longitude: { ... }
            latitude:  { ... }
            postalCode:  { ... }
            countryCode: { ... }
            city:  { ... }
            state: { ... }
          anyOf:
            - required: [longitude, latitude]
            - required: [postalCode, countryCode]
            - required: [city, state, countryCode]

     

    Note that OpenAPI 3.0 does not support the dependencies keyword of JSON Schema, but it's supported in OpenAPI 3.1 (the latest version).

     

    There's also an existing feature request in the OpenAPI Specification repository to support dependencies between individual parameter definitions.