swagger-api / swagger-api/swagger-codegen

Improve generation of object composition/inheritance on properties

Open
#9,374 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When a model is generated using swagger-codegen for openapi 3.0, if the specification contains inheritance/composition on a property, the generated model does not link the different classes properly.

This behaviour has been observed in the Kotlin and Swift4 generated models, but probably affects all the languages.

Swagger-codegen version

swagger-codegen 3.0.7

Swagger declaration file content or url
components:
  schemas:
    BaseModel:
      type: object
      properties:
        title:
          type: string
    ExtendedModel:
      properties:
        attributes:
          allOf:
            - $ref: '#/components/schemas/BaseModel'
            - type: object

The above specification produces these two classes:

public struct BaseModel: Codable {
    public var title: String?
    public init(title: String?) { 
        self.title = title
    }
}
public struct ExtendedModel: Codable {
    public var attributes: Object?
    public init(attributes: Object?) { 
        self.attributes = attributes
    }
}

As this example shows, the ExtendedModel class contains a property of type Object (that by the way does not exist in Swift) instead of pointing to the BaseModel class defined before.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the OpenAPI 3.0 schema shown in the issue with the swagger-codegen 3.0.7 generation entry point, focusing on the Kotlin and Swift4 outputs. Trace how an allOf property reference is resolved and compare the generated ExtendedModel property with the referenced BaseModel. Done means the property uses the appropriate generated model instead of Object, with behavior verified for the reported generators.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi, swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.