Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI 3.x: add support for getting example values in request body for schemas with anyOf and allOf keywords when there are other properties #9763

Closed
glowcloud opened this issue Mar 28, 2024 · 1 comment

Comments

@glowcloud
Copy link
Contributor

glowcloud commented Mar 28, 2024

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.3
info:
  title: Example app
  description: Example
  version: 1.0.0
  contact:
    name: John Doe
    email: [email protected]
servers:
  - url: https://dev.app.com/appApplicationName
    description: DEV
paths:
  /documentsWithCombineOneOf:
    post:
      description: ''
      summary: Add a document - KO
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/ModelPostDocumentOneOfCombineModelFile'
      operationId: testOneOf
      security:
        - Basic: []
      tags: []
      responses:
        '200':
          $ref: '#/components/responses/Default'
components:
  schemas:
    AbstractModelPostDocument:
      type: object
      properties:
        title:
          type: string
          default: ''
          maxLength: 64
          example: MyDocument.pdf'
        category:
          type: string
          pattern: ''
        type:
          type: string
          description: Type depends on category
        issuingApplicationId:
          type: string
          enum:
            - App1
            - App2
            - App3
            - App4
        documentDate:
          type: string
          description: >-
            Creation date of the document. Example : For ID card, it is the
            issuing date of the card.
          format: date
        sharedWith:
          type: array
          items:
            type: string
            enum:
              - CustomerPortal
              - PartnerPortal
          uniqueItems: true
          minItems: 0
          maxItems: 2
      required:
        - title
        - category
        - type
        - documentDate
        - file
    ModelPostDocumentPolicy:
      allOf:
        - $ref: '#/components/schemas/AbstractModelPostDocument'
        - type: object
          properties:
            policyNumber:
              type: string
              description: Policy Number
              example: '132456'
              minLength: 1
              maxLength: 36
              pattern: ''
          description: Metadata for policy documents
          required:
            - policyNumber
    ModelPostDocumentCustomer:
      allOf:
        - $ref: '#/components/schemas/AbstractModelPostDocument'
        - type: object
          properties:
            customerNumber:
              type: number
              description: Customer Number
              exclusiveMinimum: true
              minimum: 0
              exclusiveMaximum: true
              maximum: 99999999999999
            organisationName:
              type: string
              enum:
                - Organisation1
                - Organisation2
                - Organisation3
              description: Organisation Name
          required:
            - organisationName
            - customerNumber
          description: Metadata for Customer Documents
    ModelPostDocumentInvoice:
      allOf:
        - $ref: '#/components/schemas/AbstractModelPostDocument'
        - type: object
          properties:
            invoiceNumber:
              type: string
              description: Invoice Number
              example: '132456'
              minLength: 1
              maxLength: 36
              pattern: ''
            amount:
              type: number
              example: 1234.56
              description: Invoice Amount
          description: Metadata for invoice documents
          required:
            - amount
            - invoiceNumber
    ModelPostDocumentOneOf:
      oneOf:
        - $ref: '#/components/schemas/ModelPostDocumentPolicy'
        - $ref: '#/components/schemas/ModelPostDocumentCustomer'
        - $ref: '#/components/schemas/ModelPostDocumentInvoice'
      example:
        title: other.pdf
        category: Policy
        type: Policy
        issuingApplicationId: App1
        documentDate: '2021-01-01'
        sharedWith:
          - CustomerPortal
        policyNumber: '132456'
    ModelFile:
      type: object
      properties:
        content:
          type: string
          format: binary
      required:
        - content
    ModelPostDocumentOneOfCombineModelFile:
      allOf:
        - $ref: '#/components/schemas/ModelFile'
        - type: object
          properties:
            metadata:
              $ref: '#/components/schemas/ModelPostDocumentOneOf'
          required:
            - metadata
  securitySchemes:
    Basic:
      type: http
      scheme: basic
  responses:
    Default:
      description: Not needed
      headers: {}
      content:
        application/json:
          schema:
            type: string
            description: not needed
tags: []

Describe the bug you're encountering

With changes in #9762, we're introducing support for getting examples for schemas from oneOf and anyOf schemas for request body with content type application/x-www-form-urlencoded and multipart/form-data.

If we don't have any other properties in the schema, we will overwrite it with the first oneOf/anyOf schema, so that we can have properties like type, format, etc.

If we have other properties, we won't be overwriting them so that we don't lose anything from the parent schema. We will still get an example but we can have a situation where other properties, like type, are actually defined in the nested schema, and our input for it will look like this:

Screenshot 2024-03-28 at 16 19 27

instead of:

Screenshot 2024-03-28 at 16 22 38

We should add support for the case where we have other properties in the parent schema.

@glowcloud glowcloud changed the title OpenAPI 3.x: add support for getting example values in request body for schemas with anyOf and allOf keywords OpenAPI 3.x: add support for getting example values in request body for schemas with anyOf and allOf keywords when there are other properties Mar 28, 2024
glowcloud added a commit that referenced this issue Mar 29, 2024
@glowcloud
Copy link
Contributor Author

Addressed by #9767

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants