yonaskolb / SwagGen

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

Use first group schema type when there is only one group schema #217

Closed nicholascross closed 4 years ago

nicholascross commented 4 years ago

This avoids hitting the fallback to using the property name which without further intervention results in missing types when compiling generated code.

...

"ExampleModel": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "exampleProperty": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/ExampleType"
              }
            ]
          }
        }
}
...

Before

public var exampleProperty: ExampleProperty

After

public var exampleProperty: ExampleType

This change still would result in compilation errors for generated code where there was more than one possible schema for a group schema. Is there a proposed or correct way of handling this?

From what I can tell right now this will just generate the property type using a camel cased property name which seems a bit fragile - I am assuming the intention is this type be implemented manually but that seems error prone as there is the possibility of type collisions.

yonaskolb commented 4 years ago

Thanks @nicholascross. It's strange to have a spec with just a single oneOf, but I guess if you do this makes it better. Could you add a changelog as well, and a test case to https://github.com/yonaskolb/SwagGen/blob/master/Specs/TestSpec/spec.yml

Generating oneOf types hasn't been implemented yet, but could be done by generating an enum with associated types within the containing type.

nicholascross commented 4 years ago

Thanks @yonaskolb! I will make the requested changes in the near future.

The swagger spec I am using is automatically generated from server side code and I think it comes into play anytime there is some kind of inheritance going on in the backend models.

I like the idea of enum for greater support of this part of the spec. I might have a crack at it when I get some free time.

ymhuang0808 commented 3 years ago

Hi, @nicholascross Excuse me, could you take a look at the issue #267 related with the PR? I have a question about the allOf with inline schema. Thank you.