OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.53k stars 6.27k forks source link

[BUG][typescript-*][possibly other languages] Missing fields when using allOf composition #16150

Open Digirik opened 11 months ago

Digirik commented 11 months ago
Description

When working on an enhancement of the typescript-axios generation I've noticed fields missing in the generated classes. This PR changed the way inline schemas are handled. This was done among other things in order to remove dangling/useless allOf classes that existed in e.g. java. During this commit changes to the typescript examples have been comittet that removed the allOf classes withouth regard of them now missing fields. This can for e.g. be seen when comparing the generated flat-stock-pick-order-dto.ts with the corresponding spec file.

I've tried using --inline-schema-name-defaults REFACTOR_ALLOF_INLINE_SCHEMAS=true with the locally build jar of the current master but couldn't get that to work. I'll include my command line arguments used, I probably did something wrong.

This also seems to be a problem for the atleast the typescript-rxjs/allOf-composition example aswell, where SuperBoy, SuperBaby & Superman are missing properties now.

openapi-generator version

Current master

OpenAPI declaration file content or url

Spec File

Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g typescript-axios -i modules/openapi-generator/src/test/resources/3_0/typescript-axios/with-separate-models-and-api-inheritance.yaml -o samples/client/others/typescript-axios/with-separate-models-and-api-inheritance --additional-properties=withSeparateModelsAndApi=true --additional-properties=apiPackage=api --additional-properties=modelPackage=model --additional-properties=legacyDiscriminatorBehavior=false --inline-schema-name-defaults REFACTOR_ALLOF_INLINE_SCHEMAS=true

Steps to reproduce
  1. Check out the current master of the openapi-generator project
  2. Build the project
  3. Either run previously mentioned command line arguments for genrator or generate the mentioned example using ./bin/generate-samples.sh ./bin/configs/typescript-axios-with-separate-models-and-api-inheritance.yaml
  4. Compare the generated files and the spec to see that fields are missing in the generated files.
Related issues/PRs

Related Pull Requests: #15682

Suggest a fix/enhancement

If --inline-schema-name-defaults REFACTOR_ALLOF_INLINE_SCHEMAS=true works (which again maybe i was just too dumb to get to work) and fixes this issue it should probably be enabled by default for the typescript generators the same way it is for the csharp client generators as mentioned in the PR. Additionally, tests should be added that check if properties are missing in generated types. As *typescript-axios/with-separate-models-and-api-inheritance was added by our team we would be willing to add those in a PR once the issue has been resolved.

wing328 commented 11 months ago

in the latest master, the option name is inlineSchemaOptions, e.g.

--inline-schema-options ARRAY_ITEM_SUFFIX=_array_item,MAP_ITEM_SUFFIX=_map_item,RESOLVE_INLINE_ENUMS=true

https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#inline-schema-naming

wing328 commented 11 months ago

I did a test with java client generator:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/typescript-axios/with-separate-models-and-api-inheritance.yaml -o /tmp/java23/

but don't see missing properties. Maybe I missed missing.

What properties are missing?

Digirik commented 11 months ago

Hi, thank you for the quick response. I just ran it with the new option name and this time the tool ran without throwing an error. Yet it didn't fix the problem.

I think it is only broken for functional languages that don't have inheritance, e.g. typescript/js. When using the typescript-axios sample (and using the typescript axios generator) it used to create the following:

export type FlatStockPickOrderDto = AbstractFlatStockPickOrderBaseDto & FlatStockPickOrderDtoAllOf; where AbstractFlatStockPickOrderBaseDto and FlatStockPickOrderDtoAllOf were interfaces that had the corresponding fields. Now the generator generates export type FlatStockPickOrderDto = AbstractFlatStockPickOrderBaseDto; and the properties blockedUntil and blockedById which previously were defined in interface FlatStockPickOrderDtoAllOf are missing.

wing328 commented 11 months ago

I assume you've tried to use the rule/option REFACTOR_ALLOF_INLINE_SCHEMAS to fallback to previous behaviour but it still didn't work as expected, right?

Digirik commented 11 months ago

I just did that right now, before that i just copied your answer, sorry. With REFACTOR_ALLOF_INLINE_SCHEMAS it works the way it did before now. Thus my proposition of making it the default behaviour still stands.

wing328 commented 11 months ago

ok. please use the rule/option for fallback for the time being.

Do you know if all TS generators need this fallback or just typescript-axios needs the inline schemas created automatically?

wing328 commented 11 months ago

cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

Digirik commented 11 months ago

As far as i can see it atleast also affects the typescript-rxjs generator. I talked abouth this with a corworker and he suspects that probably all functional languages need the fallback.

Mettbrot commented 5 months ago

This problem still persists with the latest version 7.2.0 in typescript-axios generator. Using Dto: allOf -BaseDto -properties: ...) generates a model like this export type Dto = BaseDto; so the additional properties obviously do not land in the Dto, it only has the properties of BaseDto.

Mettbrot commented 2 months ago

This problem is still present with the latest version 7.4.0

codeart1st commented 2 months ago

This problem is still present with the latest version 7.4.0

Same boat. We still use 6.6.0 because of this issue.

Mettbrot commented 2 months ago

This issue is still in Version 7.5.0

macjohnny commented 2 months ago

This issue is still in Version 7.5.0

you are welcome to send a PR to fix this

bobvandevijver commented 1 month ago

Also hit by this issue, but it can be solved by setting REFACTOR_ALLOF_INLINE_SCHEMAS to true within the inlineSchemaOptions.

codeart1st commented 1 month ago

Also hit by this issue, but it can be solved by setting REFACTOR_ALLOF_INLINE_SCHEMAS to true within the inlineSchemaOptions.

Good call

Milananas commented 2 days ago

Maybe I am missing something, but I have tried setting this option, yet it does not solve the issue for me. I've setup a reproduction repo here: https://github.com/Milananas/openapi-generator-example

After running generate.sh it produces the following code:

/**
 * @type Test200Response
 * @export
 */
export type Test200Response = ApiResult;

If I set withSeparateModelsAndApi to false it does produce the correct interfaces, but I prefer the separated version for multiple reasons.

I'm running generator version 7.6.0 (latest as of writing).