Skip to content

Conversation

@Nukesor
Copy link
Contributor

@Nukesor Nukesor commented Jun 2, 2022

This PR fixes #12510

Previously, the generated code tried to create an any object with all possible variants of the oneOf construct, if no descriminator was specified. I couldn't find out how to use a descriminator though, as this doesn't seem to be documented in the openapi standard.

Anyhow, the old behavior didn't make a lot of sense and the compiler also failed to build this code.

As a fix, the newly generated code now only returns the result of the ToJSON function for the given variant.
I researched how to check for identity during runtime in TS, and it seems that the only way to do this is via custom typeguards functions?

Hence, I created a typeguard function for each interface in the style of instanceOf{{classname}}.

The PR is split into two commits:

  • The actual changes.
  • The update of the samples.

I'm not sure if I'm supposed to ping the whole TS team, but the checklist talks about "members", so here we go 😅 :
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir
@petejohansonxo @amakhrov @davidgamero @mkusaka

Sorry if that wasn't correct.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (6.0.1) (patch release), 6.1.x (breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your contribution!

@Nukesor Nukesor force-pushed the typescript-fetch-fix-one-of branch from 99582b9 to 10dceb1 Compare June 2, 2022 16:46
@Nukesor Nukesor force-pushed the typescript-fetch-fix-one-of branch from 10dceb1 to 161bb36 Compare June 2, 2022 16:55
Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Nukesor
Copy link
Contributor Author

Nukesor commented Jun 13, 2022

It would be great to get this merged into the next patch release :)

How does the merge flow in this project look like? Do you use merge windows?

@macjohnny macjohnny merged commit ef583c5 into OpenAPITools:master Jun 13, 2022
@wing328 wing328 added this to the 6.0.1 milestone Jul 2, 2022
Comment on lines +55 to 64

{{^discriminator}}
return { {{#oneOf}}...{{{.}}}ToJSON(value){{^-last}}, {{/-last}}{{/oneOf}} };
{{#oneOf}}
if (instanceOf{{{.}}}(value)) {
return {{{.}}}ToJSON(value as {{{.}}});
}
{{/oneOf}}

return {};
{{/discriminator}}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely familiar with this code, but I think something similar should also be added to modelGeneric.mustache.
In a project of mine where I use allOf for inheritance. The base class has a {class}FromJSONTyped function that handles all the discriminator cases, but {class}ToJSON doesn't include any discriminator code at all and just returns an object with properties of the base class.

I'm currently patching the generated code to add ignoreDiscriminator to the ToJSON functions which will be set to true when invoked from the child class's ToJSON, if false ToJSON in the base class calls the respective child class ToJSON based on the discriminator.

Here are the relevant models of my openapi config:

      "Activity": {
        "required": ["$type", "id", "title"],
        "type": "object",
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "id": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "$type",
          "mapping": {
            "exercise": "#/components/schemas/ActivityExercise",
            "job": "#/components/schemas/ActivityJob",
          }
        }
      },
      "ActivityExercise": {
        "required": ["$type", "category"],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Activity"
          }
        ],
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "category": {
            "$ref": "#/components/schemas/ActivityCategory"
          },
          "muscleShares": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MuscleShare"
            },
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "ActivityJob": {
        "required": ["$type"],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Activity"
          }
        ],
        "properties": {
          "$type": {
            "$ref": "#/components/schemas/ActivityType"
          },
          "muscleShares": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MuscleShare"
            },
            "nullable": true
          }
        },
        "additionalProperties": false
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][TypeScript fetch] "oneOf" in query parameter generates wrong code

4 participants