yonaskolb / SwagGen

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

allOf property is ignored when model marked as "type: object" in spec #221

Open aldan95 opened 4 years ago

aldan95 commented 4 years ago

Two valid specifications (verified by swagger.io online validator):

openapi: 3.0.0
info:
  version: 1.0.0
  title: Swagger Petstore
tags:
  - name: pet
    description: Everything about your Pets
paths:
  /pet:
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: ""
      operationId: addPet
      responses:
        "405":
          description: Invalid input
components:
  schemas:
    Dog:
      allOf:
        - $ref: "#/components/schemas/Animal"
        - type: object
          properties:
            breed:
              type: string
    Animal:
      type: object
      discriminator:
        propertyName: className
      required:
        - className
      properties:
        className:
          type: string
        color:
          type: string
          default: red

generates public class Dog: Animal, but same spec with type: object added to Dog

openapi: 3.0.0
info:
  version: 1.0.0
  title: Swagger Petstore
tags:
  - name: pet
    description: Everything about your Pets
paths:
  /pet:
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: ""
      operationId: addPet
      responses:
        "405":
          description: Invalid input
components:
  schemas:
    Dog:
      type: object
      allOf:
        - $ref: "#/components/schemas/Animal"
        - type: object
          properties:
            breed:
              type: string
    Animal:
      type: object
      discriminator:
        propertyName: className
      required:
        - className
      properties:
        className:
          type: string
        color:
          type: string
          default: red

generates public class Dog: APIModel without properties at all

version 4.3.1

rogerluan commented 2 years ago

The specific request this issue is about was probably addressed as of v4.4.0 and v4.6.0 (after some fixes). However there's still something broken, as reported here #295, not sure if that would affect you.

aldan95 commented 2 years ago

I've tested above samples with v.4.6.0. Dog from first spec:

public class Dog: Animal {

    public var breed: String?

    public init(className: String, color: String? = nil, breed: String? = nil) {
        self.breed = breed
        super.init(className: className, color: color)
    }

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: StringCodingKey.self)

        breed = try container.decodeIfPresent("breed")
        try super.init(from: decoder)
    }

    public override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: StringCodingKey.self)

        try container.encodeIfPresent(breed, forKey: "breed")
        try super.encode(to: encoder)
    }

    override public func isEqual(to object: Any?) -> Bool {
      guard let object = object as? Dog else { return false }
      guard self.breed == object.breed else { return false }
      return super.isEqual(to: object)
    }
}

and Dog from second spec:

public class Dog: APIModel {

    public init() {
    }

    public required init(from decoder: Decoder) throws {
    }

    public func encode(to encoder: Encoder) throws {
    }

    public func isEqual(to object: Any?) -> Bool {
      guard object is Dog else { return false }
      return true
    }

    public static func == (lhs: Dog, rhs: Dog) -> Bool {
        return lhs.isEqual(to: rhs)
    }
}