-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Kotlin] Use mutable container types when 'modelMutable' is enabled #11154
Merged
4brunu
merged 6 commits into
OpenAPITools:master
from
rm3l:use_specific_kotlin_container_type_when_model_mutable_is_true
Jan 30, 2022
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1250a9
[Kotlin] Use Mutable container types when 'modelMutable' is enabled
rm3l 10d0ad9
Generate sample 'kotlin' project with mutable models
rm3l c9d2482
Generate sample 'kotlin-server' project with mutable models
rm3l 3d61382
Generate sample 'kotlin-vertx' project with mutable models
rm3l de65993
Generate sample 'ktorm-schema' project with mutable models
rm3l bdc9263
Merge branch 'master' into use_specific_kotlin_container_type_when_mo…
4brunu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
generatorName: kotlin | ||
outputDir: samples/client/petstore/kotlin-modelMutable | ||
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-client | ||
additionalProperties: | ||
artifactId: kotlin-modelMutable | ||
modelMutable: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
generatorName: kotlin-server | ||
outputDir: samples/server/petstore/kotlin-server-modelMutable | ||
library: ktor | ||
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
additionalProperties: | ||
hideGenerationTimestamp: "true" | ||
modelMutable: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
generatorName: kotlin-vertx | ||
outputDir: samples/server/petstore/kotlin-vertx-modelMutable | ||
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-vertx-server | ||
additionalProperties: | ||
modelMutable: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
generatorName: ktorm-schema | ||
outputDir: samples/schema/petstore/ktorm-modelMutable | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/ktorm-schema | ||
additionalProperties: | ||
hideGenerationTimestamp: true | ||
importModelPackageName: org.openapitools.client.models | ||
modelMutable: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,13 @@ public String getHelp() { | |
public void processOpts() { | ||
super.processOpts(); | ||
|
||
if (isModelMutable()) { | ||
typeMapping.put("array", "kotlin.collections.MutableList"); | ||
typeMapping.put("list", "kotlin.collections.MutableList"); | ||
typeMapping.put("set", "kotlin.collections.MutableSet"); | ||
typeMapping.put("map", "kotlin.collections.MutableMap"); | ||
} | ||
|
||
// optional jackson mappings for BigDecimal support | ||
importMapping.put("ToStringSerializer", "com.fasterxml.jackson.databind.ser.std.ToStringSerializer"); | ||
importMapping.put("JsonSerialize", "com.fasterxml.jackson.databind.annotation.JsonSerialize"); | ||
|
@@ -566,6 +573,12 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o | |
resp.code = "200"; | ||
} | ||
|
||
// This is necessary in case 'modelMutable' is enabled, | ||
// to prevent Spring Request handlers from being generated with | ||
// the ApiResponse annotation configured with the Mutable container type. | ||
// See https://github.com/OpenAPITools/openapi-generator/pull/11154#discussion_r793108068 | ||
resp.baseType = getNonMutableContainerTypeIfNeeded(resp.baseType); | ||
|
||
doDataTypeAssignment(resp.dataType, new DataTypeAssigner() { | ||
@Override | ||
public void setReturnType(final String returnType) { | ||
|
@@ -579,6 +592,17 @@ public void setReturnContainer(final String returnContainer) { | |
}); | ||
}); | ||
} | ||
|
||
final List<CodegenParameter> allParams = operation.allParams; | ||
if (allParams != null) { | ||
allParams.forEach(param -> | ||
// This is necessary in case 'modelMutable' is enabled, | ||
// to prevent Spring Request handlers from being generated with | ||
// parameters using their Mutable container types. | ||
// See https://github.com/OpenAPITools/openapi-generator/pull/11154#discussion_r793094727 | ||
param.dataType = getNonMutableContainerTypeIfNeeded(param.dataType)); | ||
} | ||
|
||
doDataTypeAssignment(operation.returnType, new DataTypeAssigner() { | ||
|
||
@Override | ||
|
@@ -600,6 +624,13 @@ public void setReturnContainer(final String returnContainer) { | |
return objs; | ||
} | ||
|
||
private String getNonMutableContainerTypeIfNeeded(String type) { | ||
if (type != null && type.contains("kotlin.collections.Mutable")) { | ||
return type.replaceAll("kotlin\\.collections\\.Mutable", "kotlin.collections."); | ||
} | ||
return type; | ||
} | ||
|
||
private interface DataTypeAssigner { | ||
void setReturnType(String returnType); | ||
|
||
|
@@ -619,12 +650,24 @@ private void doDataTypeAssignment(final String returnType, DataTypeAssigner data | |
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.List<".length(), end).trim()); | ||
dataTypeAssigner.setReturnContainer("List"); | ||
} | ||
} else if (returnType.startsWith("kotlin.collections.MutableList")) { | ||
int end = returnType.lastIndexOf(">"); | ||
if (end > 0) { | ||
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableList<".length(), end).trim()); | ||
dataTypeAssigner.setReturnContainer("List"); | ||
} | ||
} else if (returnType.startsWith("kotlin.collections.Map")) { | ||
int end = returnType.lastIndexOf(">"); | ||
if (end > 0) { | ||
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.Map<".length(), end).split(",")[1].trim()); | ||
dataTypeAssigner.setReturnContainer("Map"); | ||
} | ||
} else if (returnType.startsWith("kotlin.collections.MutableMap")) { | ||
int end = returnType.lastIndexOf(">"); | ||
if (end > 0) { | ||
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableMap<".length(), end).split(",")[1].trim()); | ||
dataTypeAssigner.setReturnContainer("Map"); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a case missing for "MutableSet" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @martin-regis could you please open a PR with a fix please? |
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rm3l could you please explain what this does and why it's needed? Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar reasons as in my other comment below.
For example, given the following input operation:
Without the changes here, this resulted in the following code generated by the
kotlin-spring
generator:StoreApiController.kt
: the Response field in theApiResponse
annotation has changed toMutableMap
, which, I guess, is not valid.Again, maybe this is not the right approach here or maybe I missed something, but I found no clear way to distinguish between the type mappings used for generating models (Kotlin data classes) and response types in the
kotlin-spring
generator. Also, since the original issue I reported was more related to model mutability, I thought it would be ok not to change the code generated for the Spring Controllers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining, this way we have an explanation in case we need it 👍