Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #6513
Closed
Open
Issue created Jun 01, 2020 by Administrator@rootContributor

[BUG] typescript-fetch oneOf doesn't handle discrimination by property.

Created by: keean

Description

OpenApi generator does not generate valid code for unions discriminated by property name. Unions (oneOf) work correctly when discriminated by a property value, however the generated code seems to try and combine the property lists from all members of the union. If each member of the union have disjoint sets of required properties, then the union is discriminated, and should be generated as such.

openapi-generator version

openapi-generator 4.3.1

OpenAPI declaration file content or url
{
    "openapi": "3.0",
    "info": {
        "title": "Test",
        "version": "0.1"
    },
    "servers": [{
        "url": "https://localhost"
    }],
    "paths": {
        "/": {
            "get": {
                "operationId": "dummyOperation",
                "responses": {
                    "200": {
                        "description": "Successful operaion",
                        "content": {
                            "application/json": {
                                "schema": {"$ref": "#/components/schemas/aOrB"}
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "a": {
                "type": "object",
                "required": ["a", "b"],
                "properties": {
                    "a": {
                        "type": "string"
                    },
                    "b": {
                        "type": "string"
                    }
                }
            },
            "b": {
                "type": "object",
                "required": ["b", "c"],
                "properties": {
                    "b": {
                        "type": "string"
                    },
                    "c": {
                        "type": "string"
                    }
                }
            },
            "aOrB": {
                "oneOf": [
                    {"$ref": "#/components/schemas/a"},
                    {"$ref": "#/components/schemas/b"}
                ]
            }
        }
    }
}
Command line used for generation

openapi-generator generate -i test.json -g typescript-fetch --additional-properties=typescriptThreePlus --generate-alias-as-model -o test

Steps to reproduce

After generation, the model file for aOrB is:

export function AOrBFromJSONTyped(json: any, ignoreDiscriminator: boolean): AOrB {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return { ...AFromJSONTyped(json, true), ...BFromJSONTyped(json, true) };
}

export function AOrBToJSON(value?: AOrB | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return { ...AToJSON(value), ...BToJSON(value) };
}

This fails because A and B both contain 'required' parameters, so they cannot be combined by spreading as above.

Related issues/PRs

There are similar PR, but they don't seem to address exactly the same issue: #6376 #5202 #4626 #1563 #927 (closed)

Suggest a fix

The generated functions need to discriminate on properties as well as values, something like:

export function AOrBFromJSONTyped(json: any, ignoreDiscriminator: boolean): AOrB {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    if (json['a'] != undefined && json['b'] != undefined) {
        return AFromJSONTyped(json, true);
    } else if (json['b'] != undefined && json['c'] != undefined) {
        return BFromJSONTyped(json, true);
    }
}

export function AOrBToJSON(value?: AOrB | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    if (value.a != undefined && value.b != undefined) {
        return AToJSON(value);
    } else if (value.b != undefined && value.c != undefined) {
        return BToJSON(value);
    }
}

In the 'if' statements it would test all the required parameters for that member object in the union.

Assignee
Assign to
Time tracking