openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open Digirik opened this issue 2 years ago • 16 comments

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.

Digirik avatar Jul 21 '23 06:07 Digirik

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 avatar Jul 21 '23 08:07 wing328

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?

wing328 avatar Jul 21 '23 08:07 wing328

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.

Digirik avatar Jul 21 '23 09:07 Digirik

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?

wing328 avatar Jul 21 '23 09:07 wing328

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.

Digirik avatar Jul 21 '23 09:07 Digirik

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 avatar Jul 21 '23 09:07 wing328

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)

wing328 avatar Jul 21 '23 09:07 wing328

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.

Digirik avatar Jul 21 '23 09:07 Digirik

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 avatar Jan 18 '24 09:01 Mettbrot

This problem is still present with the latest version 7.4.0

Mettbrot avatar Apr 09 '24 07:04 Mettbrot

This problem is still present with the latest version 7.4.0

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

codeart1st avatar Apr 09 '24 08:04 codeart1st

This issue is still in Version 7.5.0

Mettbrot avatar Apr 26 '24 14:04 Mettbrot

This issue is still in Version 7.5.0

you are welcome to send a PR to fix this

macjohnny avatar Apr 29 '24 07:04 macjohnny

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

bobvandevijver avatar May 17 '24 12:05 bobvandevijver

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

Good call

codeart1st avatar May 18 '24 05:05 codeart1st

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).

EDIT: It is working, I made a mistake with the setting I used. My bad, sorry for the confusion!

Milananas avatar Jun 28 '24 11:06 Milananas