Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Support for inheritance #95

Closed arkraft closed 4 years ago

arkraft commented 4 years ago

Thank you very much for creating this library!

OpenAPI 3 has support for inheritance: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

It would be greate if we could use it when generating a client. We have this situation in out backend and the generated OpenAPI file is valid and has the correct references. The generated file only contains the base type and the other types are not generated at all. I'm not asking for validation at the client side, but at least to generate the other types of oneOf and set a union type for the attribute. I would really appreciate that. Thank you!

Manweill commented 4 years ago

@arkraft it mean that the return value or parameter has two or more types?

arkraft commented 4 years ago

Yes, thats right. We currently have something like this:

"AllOccurrences": {
    "type": "object",
    "properties": {
        "occurrences": {
            "type": "array",
            "items": {
                "oneOf": [
                    {
                        "$ref": "#/components/schemas/OvertimeOccurrenceDto"
                    },
                    {
                        "$ref": "#/components/schemas/AbsenceOccurrenceDto"
                    },
                    {
                        "$ref": "#/components/schemas/NotPlannedOccurrenceDto"
                    },
                    {
                        "$ref": "#/components/schemas/TooEarlyOccurrenceDto"
                    },
                    {
                        "$ref": "#/components/schemas/TooLateOccurrenceDto"
                    }
                ],
                "discriminator": {
                    "propertyName": "kind"
                }
            }
        }
    },
    "required": [
        "occurrences"
    ]
}

So occurences is an array of the types OvertimeOccurrenceDto, AbsenceOccurrenceDto, NotPlannedOccurrenceDto, TooEarlyOccurrenceDto and TooLateOccurrenceDto. The expected result in the interface would be:

// expected
occurrences: (OvertimeOccurrenceDto | AbsenceOccurrenceDto | NotPlannedOccurrenceDto | TooEarlyOccurrenceDto | TooLateOccurrenceDto)[];

// actual
occurrences: any | null[];
arkraft commented 4 years ago

I am currently working on this myself

arkraft commented 4 years ago

I created #96 to fix this issue. There is an allOf inside the schema but in the end only the type of the first element of the allOf was used. This will be a breaking change because not all types of the allOf are combined to one type which needs to include all types. But i believe that this is the way it should be, i checked it with the swagger editor and created an angular client, it was like that.

Like i said, this contains a breaking change and i don't know how this should be handled

Manweill commented 4 years ago

Thank you very much for your contribution.