papsign / Ktor-OpenAPI-Generator

Ktor OpenAPI/Swagger 3 Generator
Apache License 2.0
241 stars 42 forks source link

Fix for OneOf Subtyping - Generators were failing to generate proper discriminators #90

Closed pixellos closed 3 years ago

pixellos commented 3 years ago

Tested on generators:

Given:

Whe:

Then

Should:

Old behavior: Generated code with discriminators not compiling image

No discriminators:


export class Base {
    'str': string;
    'i': number;

    static readonly discriminator: string | undefined = undefined;

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "str",
            "baseName": "str",
            "type": "string",
            "format": ""
        },
        {
            "name": "i",
            "baseName": "i",
            "type": "number",
            "format": "int32"
        }    ];

    static getAttributeTypeMap() {
        return Base.attributeTypeMap;
    }

    public constructor() {
    }
}

export class A {
    'str': string;

    static readonly discriminator: string | undefined = undefined;

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "str",
            "baseName": "str",
            "type": "string",
            "format": ""
        }    ];

    static getAttributeTypeMap() {
        return A.attributeTypeMap;
    }

    public constructor() {
    }
}

Expected Behavior (current):


export class Base {
    'type'?: string;
    'i': number;
    'l': number;

    static readonly discriminator: string | undefined = "type";

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "type",
            "baseName": "type",
            "type": "string",
            "format": "string"
        },
        {
            "name": "i",
            "baseName": "i",
            "type": "number",
            "format": "int32"
        },
        {
            "name": "l",
            "baseName": "l",
            "type": "number",
            "format": "int64"
        }    ];

    static getAttributeTypeMap() {
        return Base.attributeTypeMap;
    }

    public constructor() {
        this.type = "Base";
    }
}

export class B {
    'i': number;
    'type'?: string;

    static readonly discriminator: string | undefined = "type";

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "i",
            "baseName": "i",
            "type": "number",
            "format": "int32"
        },
        {
            "name": "type",
            "baseName": "type",
            "type": "string",
            "format": "string"
        }    ];

    static getAttributeTypeMap() {
        return B.attributeTypeMap;
    }

    public constructor() {
        this.type = "B";
    }
}
pixellos commented 3 years ago

@Wicpar Can U throw an eye?

Wicpar commented 3 years ago

Looks good to me, not too happy about the formatting changes though as it makes it harder to see the actual changes, but that's ok.