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
21.33k stars 6.46k forks source link

[REQ][Java][Spring] Process and generate variables/objects (and their annotations) recursively in mustache #19601

Open DatApplePy opened 2 days ago

DatApplePy commented 2 days ago

Is your feature request related to a problem? Please describe.

Currently, all container objects (List, Set and Map) with their elements are processed as one in the generator (AbstractJavaCodegen). Example:

testList:
  type: array
  maxItems: 10
  items:
    type: array
    maxItems: 5
    uniqueItems: true
    items:
      type: string
      pattern: "^[0-9]{10,15}$"

datatypeWithEnum value of testList: List<@Size(max = 5) Set<@Pattern(regexp = "^[0-9]{10,15}$") String>>

Generated code:

@Size(max = 10)
private List<@Size(max = 5) Set<@Pattern(regexp = "^[0-9]{10,15}$") String>> testList;

This leads to 2 problems (that's all I've found so far): 1) Cannot create and apply custom annotations 2) Custom messages cannot be applied to Jakarta annotations

Describe the solution you'd like

It would be better if the dataTypeWithEnum value of the variables contained only the type of the current schema. Using the previous example, the result would be this:

name: testList
dataTypeWithEnum: List
isContainer: true
isArray: true
uniqueItems: false
...
items:
  dataTypeWithEnum: Set
  isContainer: true
  isArray: true
  uniqueItems: true
  ...
  items:
    dataTypeWithEnum: String

This way we can generate these structures recursively and also apply custom annotation and more.

Additional context

Since this issue is the extended form of another issue (#19557), I am already working on the feature. I planned to implement the feature in two stages: 1) Refactor mustache templates related to this issue

Note: The development process can be slow because I read a lot of documentation (and StackOverFlow) and experiment a lot to make everything work as intended.

DatApplePy commented 2 days ago

First stage: https://github.com/OpenAPITools/openapi-generator/compare/master...DatApplePy:openapi-generator:fix_issue_19601