yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
626 stars 146 forks source link

The behavior of inline schema for allOf #267

Open ymhuang0808 opened 3 years ago

ymhuang0808 commented 3 years ago

In version 4.4.0, there is a bug about the inline schema for allOf. In the PR #217 , according to the revision of Sources/SwagGenKit/SwaggerExtensions.swift, allOf cannot be generated in a certain situation. The if condition is true when it ONLY occurs in anyOf or oneOf, because the discriminator will never appears with allOf.

The below OpenAPI example spec shows the category inline schema is not generated in Pet .

components:
  schemas:
    Operator:
      required:
        - operation
      properties:
        operation:
          type: string
          enum:
            - remove
            - replace
    Pet:
      required:
      - name
      - photoUrls
      - category
      type: object
      properties:
        id:
          type: integer
          format: int64
        category:
          allOf:
            - $ref: "#/components/schemas/Operator"
            - properties:
                value:
                  type: object
                  required: 
                    - id
                  properties:
                    id:
                      type: string
        name:
          type: string
          example: doggie
        photoUrls:
          type: array
          xml:
            name: photoUrl
            wrapped: true
          items:
            type: string
        status:
          type: string
          description: pet status in the store
          enum:
          - available
          - pending
          - sold

I expect the the SwagGen will generate a inner Category class in Pet class model. In fact, the Category are not generated.

nicholascross commented 3 years ago

Hi @ymhuang0808 are you suggesting this #217 caused some kind of regression or that there is additional consideration required to support allOf inline as well?

Apologies my head has been out of this code for too long to offer much meaningful insight.

If it was a regression I wonder if it would be resolved by adding an else if to consider all separately.

if group.discriminator != nil {
  return self
} else if case .all = group.type {
  // might restore previous consideration of all based on group type
  return self
}
ymhuang0808 commented 3 years ago

Hi @nicholascross Thanks for your kind reply. Yes, the PR #217 makes the inline class for allOf broken. The code snippet works for the allOf in inline class. Would you like to make a pull request?

Cheers! :smiley:

liamnichols commented 2 years ago

Hi both šŸ‘‹ I think there is a little more involved here and i submitted a patch in #278... Would be great if you could check it out šŸ™