swagger-api / swagger-inflector

Apache License 2.0
160 stars 84 forks source link

Support anyOf, oneOf, and not in ExampleBuilder in 3.0 #215

Closed gracekarina closed 6 years ago

gracekarina commented 6 years ago

Support anyOf, oneOf, and not in ExampleBuilder in 3.0

Right now inflector only supports allOf.

hkosova commented 6 years ago

Assuming ExampleBuilder is supposed to return the first subschema from anyOf and oneOf,

given this spec

openapi: 3.0.0
info:
  version: 0.0.0
  title: test

paths:
  /oneOf:
    get:
      responses:
        '200':
          description: A book or movie object
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Book'
                  - $ref: '#/components/schemas/Movie'

  /anyOf:
    get:
      responses:
        '200':
          description: A book or movie object
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: '#/components/schemas/Movie'
                  - $ref: '#/components/schemas/Book'

  /mixed-array:
    get:
      responses:
        '200':
          description: An array containing strings and/or integers
          content:
            application/json:
              schema:
                type: array
                items:
                  oneOf:
                    - type: string
                    - type: integer

components:
  schemas:
    Book:
      type: object
      properties:
        title:
          type: string
        authors:
          type: array
          items:
            type: string
        isbn:
          type: string
      required:
        - title
      example:
        title: The Hitchhiker's Guide to the Galaxy
        authors:
          - Douglas Adams
        isbn: 0-330-25864-8
    Movie:
      type: object
      properties:
        title:
          type: string
        directors:
          type: array
          items:
            type: string
        year:
          type: integer
      required:
        - title
      example:
        title: Blade Runner
        directors:
          - Ridley Scott
        year: 1982

/oneOf should return a book:

{
  "title": "The Hitchhiker's Guide to the Galaxy",
  "authors": [
    "Douglas Adams"
  ],
  "isbn": "0-330-25864-8"
}

/anyOf should return a movie:

{
  "title": "Blade Runner",
  "directors": [
    "Ridley Scott"
  ],
  "year": 1982
}

and /mixed-array should return a string array:

[
  "string"
]
gracekarina commented 6 years ago

@hkosova Thanks Helen, do you have a spec with not

hkosova commented 6 years ago

@gracekarina

openapi: 3.0.0
info:
  version: 0.0.0
  title: not models
paths:
  /NotString:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                $ref: '#/components/schemas/NotString'
  /NotStringInlineSchema:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                not:
                  type: string
  /NotNumber:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                $ref: '#/components/schemas/NotNumber'
  /NotNumberInlineSchema:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                not:
                  anyOf:
                    - type: number
                    - type: integer
  /ArrayOfNotString:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                $ref: '#/components/schemas/ArrayOfNotString'
  /ArrayOfNotStringInlineSchema:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                type: array
                items:
                  not:
                    type: string
  /NotUserObject:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                $ref: '#/components/schemas/NotUser'
  /NotUserObjectInline:
    get:
      responses:
        '200':
          description: OK
          content:
            application/yaml:
              schema:
                not:
                  $ref: '#/components/schemas/User'
components:
  schemas:
    NotString:
      not:
        type: string
    NotNumber:
      not:
        anyOf:
          - type: number
          - type: integer
    NotUser:
      not:
        $ref: '#/components/schemas/User'
    ArrayOfNotString:
      type: array
      items:
        not:
          type: string

    User:
      type: object
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string
      required: [id, name]
gracekarina commented 6 years ago

the inflector currently supports anyOf and OneOf, the not is not supported yet.

gracekarina commented 6 years ago

support of not, done!