1

I didn't see anything specifically pertaining to this in the OpenAPI Reference, so I wanted to ask here for confirmation.

Let's say I have a reference with the following data:

    components:
      schemas:
        Foobar:
          type: object
          properties:
            timestamp:
              type: number
            uid:
              type: number
            username:
              type: string
            location:
              type: string

Is it possible to reference this component but only take specific properties from within, such as uid and username, and exclude the rest? e.g.

$ref: '#/components/schemas/Foobar(uid,username)'

My use case is that I have a library call that returns records from the database, adding (or removing) additional records as necessary based on user input. So we could have myCall(['username']); returning just the username, and myCall(['username', 'location']); returning username and location.

If I wanted to properly document its various usages within my API, I would currently have to manually maintain all of the different variants of the output.

I was hoping I could make some "flexible" component I could reference, and have it still validate within the OpenAPI spec.

The closest I could find would be to wrap all of the properties under anyOf:

    components:
      schemas:
        Foobar:
          anyOf:
            - type: object
              properties:
                timestamp:
                  type: number
                uid:
                  type: number
                username:
                  type: string
                location:
                  type: string

... but if parsed by something like ReDoc, it would just show that the output would be any of the listed properties, when I actually need the ability to declare that "out of any of these properties, the following will be output".

I'm guessing my use case is a little esoteric, but I am hoping to be proven wrong :)

Julian H. Lam
  • 25,501
  • 13
  • 46
  • 73
  • I am not sure it's possible with OAS. `..adding (or removing) additional records as necessary based on user input..` then would it be possible to split the model into more granular pieces as a workaround? – Sasha Dec 14 '21 at 15:22
  • See the [linked Q&A](https://stackoverflow.com/q/57339131/113116). You do this the other way round: define the base schema with the _minimum set_ of properties, then define the extended schema as `allOf` of the base schema plus extra properties. – Helen Dec 14 '21 at 19:07

0 Answers0