Add nameMapping option to Java client, server generators (#16103)

* add nameMapping to java codegen

* update doc

* update samples

* fix typo

* update toParamName
This commit is contained in:
William Cheng
2023-07-17 11:37:49 +08:00
committed by GitHub
parent b107ff96ac
commit 6983a3acf3
23 changed files with 652 additions and 3 deletions

View File

@@ -395,11 +395,34 @@ or
--import-mappings Pet=my.models.MyPet --import-mappings Order=my.models.MyOrder
```
## Name Mapping
One can map the name of the property/parameter to something else. Consider the following schema:
```
PropertyNameCollision:
properties:
_type:
type: string
type:
type: string
type_:
type: string
type: object
```
`_type`, `type`, `type_` will result in property name collision in the Java client generator for example. We can resolve the issue using `nameMappings` by mapping `_type` to `underscoreType`, `type_` to `typeWithUnderscore`.
Here is an example to use `nameMappings` in CLI:
```sh
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml -o /tmp/java2/ --name-mappings _type=underscoreType, type_=typeWithUnderscore
```
(Not all generators support this feature yet. Please open an issue (ticket) to let us know which generators you would like to have this feature enabled and we'll prioritize accordingly.)
## Schema Mapping
One can map the schema to something else (e.g. external objects/models outside of the package) using the `schemaMappings` option, e.g. in CLI
```sh
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/type-alias.yaml -o /tmp/java2/ --schema-mapping TypeAlias=foo.bar.TypeAlias
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/type-alias.yaml -o /tmp/java2/ --schema-mappings TypeAlias=foo.bar.TypeAlias
```
Another example (in conjunction with --type-mappings):
```sh