user12331
user12331

Reputation: 518

Make some of the query parameters dependent in RAML

I am writing an api spec with RAML. I have a GET api with three query parameters. One of them is optional. In the case of other two, one of them should be present at least. How do I define this in the api spec

/api
description: api end point

queryParameters:
  field1:
    description: field1
    required: false
    type: string
  field2:
    description: field2
    required: false
    type: string
  field3:
    description: field3
    required: false
    type: string

Here field1 is completely optional. It is ok if we don't have it. But out of the other two, either field2 or field3 should be present.

So the api call should be

/api?field2=value or /api?field3=value

How do I do this in raml?

Upvotes: 2

Views: 6545

Answers (2)

user19592504
user19592504

Reputation: 1

If you want to force so that the bad request is returned in case of no parameter is passed, then you can try to use minProperties:

{types:
  oneParam:
    **minProperties: 1**
    properties:
      field1:
        description: field1
        required: false
        type: string
      field3:
        description: field3
        type: string
  otherParam:
    **minProperties: 1**
    properties:
      field1:
        description: field1
        required: false
        type: string
      field2:
        description: field2
        type: string

/api:
  description: api end point

  get:
    queryParameters:
      type: oneParam | otherParam
}

Upvotes: 0

DarkRodry
DarkRodry

Reputation: 31

You can split the query params in two types (one for every combination) and use a union type:

types:
  oneParam:
    properties:
      field1:
        description: field1
        required: false
        type: string
      field3:
        description: field3
        type: string
  otherParam:
    properties:
      field1:
        description: field1
        required: false
        type: string
      field2:
        description: field2
        type: string

/api:
  description: api end point

  get:
    queryParameters:
      type: oneParam | otherParam

Upvotes: 3

Related Questions