4

Following the example of an OpenAPI 3 definition:

components:
  schemas:
    Foo:
      properties:
        string:
          type: string
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']
    Bar:
      properties:
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']

Is there a way to reuse enumField or do I have to specify it every time I use it?

Hannes
  • 5,002
  • 8
  • 31
  • 60
  • Possible duplicate of [Property reference to Model on swagger 2.0 (nesting)](https://stackoverflow.com/questions/26287962/property-reference-to-model-on-swagger-2-0-nesting) – Helen Apr 15 '19 at 08:39
  • 2
    You need to define a schema for that enum, then you can `$ref` the enum schema in the `enumField` property. See the ^^ linked question for the `$ref` syntax. – Helen Apr 15 '19 at 08:40
  • Related: [Swagger: Reusing an enum definition as query parameter](https://stackoverflow.com/q/32255384/113116) – Helen Apr 15 '19 at 08:41

1 Answers1

8

I don't know if is possible to reference a single property, but you can do it with schemas, and by spliting it.

Here is a basic structure sample of what can you do:

SchemaBase:
  type: object
  properties:
    foo:
      type: string

SchemaFull:
  allOf:
    - $ref: '#/components/schemas/SchemaBase'
    - type: object
      properties:
        bar:
          type: string

In your case could be something like this:

components:
  schemas:
    EnumField:
      properties:
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']
    Foo:
      allOf:
        - $ref: '#/components/schemas/EnumField'
        - properties:
            string:
              type: string
    Bar:
      allOf:
        - $ref: '#/components/schemas/EnumField'