Skip to content

[BUG][typescript-axios] missing discriminator#5263

Open
cervotoc wants to merge 1 commit intoOpenAPITools:masterfrom
marbes-consulting:fix-typescript-axios-discriminator
Open

[BUG][typescript-axios] missing discriminator#5263
cervotoc wants to merge 1 commit intoOpenAPITools:masterfrom
marbes-consulting:fix-typescript-axios-discriminator

Conversation

@cervotoc
Copy link

@cervotoc cervotoc commented Feb 10, 2020

Extended musatche template to include generating of discriminator.

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

fixes #5262

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02)

@macjohnny macjohnny changed the title Fix for #5262 [BUG][typescript-axios] missing discriminator [BUG][typescript-axios] missing discriminator Feb 10, 2020
@macjohnny macjohnny added this to the 4.3.0 milestone Feb 10, 2020
@amakhrov
Copy link
Contributor

Looks like this change would put the discriminator property twice in the generated interface, if the property is explicitly listed in api spec. This is the case in the examples provided in OAS3, e.g.

      "Pet": {
        "type": "object",
        "discriminator": {
          "propertyName": "petType"
        },
        "properties": {
          "name": {
            "type": "string"
          },
          "petType": {
            "type": "string"
          }
        },
        "required": [
          "name",
          "petType"
        ]
      }

@amakhrov
Copy link
Contributor

Shouldn't a fix involve adding a missing implicit discriminator into allProps in DefaultCodegen instead? And only in a case when it is missing indeed.
How do other generators handle this scenario?

@macjohnny
Copy link
Member

@wing328 can you help here?

@cervotoc
Copy link
Author

cervotoc commented Feb 12, 2020

Ok, I agree. But when I add the discriminator as a property, the server side (openapi generator: spring) adds that property to class and I don't want that. I just want an annotation for discriminator @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@class", visible = true) on Java classes.

@amakhrov
Copy link
Contributor

@cervotoc I see.
Apparently, in Java discriminator is emitted separately from model properties, hence no collision.

However, in TS you're emitting it as a regular model property. So there must be some flag in the model determining whether we need to add the discriminator as a new property into the generated model or not. Or just add this unconditionally in the Typescript Client (not in the default codegen, as it would break the existing logic for java and probably something else).

The problem is that having the discriminator as a model property in api-spec is a totally valid use case, and is demonstrated in many examples in OAS2 and OAS3. Hence we must make sure the codegen still generates valid typescript code for this case - as well as for your case with no explicit property for discriminator.

@wing328
Copy link
Member

wing328 commented Feb 24, 2020

@wing328 can you help here?

I'll take a look later this week.

@wing328
Copy link
Member

wing328 commented Feb 24, 2020

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@cervotoc
Copy link
Author

Can some one help with #5694 / #5695 ? Nobody response :\

@cervotoc cervotoc requested a review from macjohnny April 23, 2020 09:22
@wing328 wing328 removed this from the 4.3.1 milestone May 6, 2020
@macjohnny
Copy link
Member

@amakhrov what do you suggest to do here?

@amakhrov
Copy link
Contributor

@macjohnny I could imagine having additional logic in AbstractTypeScriptClient#postProcessModels:

  • for each model
    • if it has discriminator, check if the property with the same name already exists in properties (allProperties?)
    • if exists, do nothing
    • if doesn't exist - add it as a property of type string

@Mettbrot
Copy link

Mettbrot commented May 2, 2025

@cervotoc @amakhrov What is the status of this? We are hitting this exact problem. The inheritance support of the typescript-axios generator is not really complete. In addition to the generation of the discriminator property in the interface, it would be necessary to automatically set it to the interfaces name. How have you since solved this?

I also tried it with const, but it does not work correctly either:

BaseClass:
  discriminator:
    propertyName: className

ChildClass:
  allOf:
    - BaseClass
    - type: object
      properties:
        className:
          const: "ChildClass"

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][typescript-axios] missing discriminator

5 participants