Open PierreMardon opened 9 months ago
In fact, my previous suggestion won't work. Replacing the Map
with kotlinx.serialization.json.JsonObject
works and seems appropriate.
How did you doReplacing the Map with kotlinx.serialization.json.JsonObject
? @PierreMardon
@charlee-dev I'm far from being an expert so there may be a better way to do so, but as I needed multiple post-generation tweaks I implemented a copy task:
tasks.register<Copy>("postProcessDto") {
from(generatedDtoFolder)
into(targetDtoFolder)
filter { line: String ->
line
// Fix for https://github.com/OpenAPITools/openapi-generator/issues/17658
.replace(
"kotlin.collections.Map<kotlin.String, kotlin.Any>",
"kotlinx.serialization.json.JsonObject"
)
// We need plain old java Date
.replace("java.time.OffsetDateTime", "java.util.Date")
}
dependsOn("openApiGenerate")
}
In case, that you don't need to copy DTOs to different folder, you can use edited task:
val dtoPath = "$projectDir/src/main/kotlin/.../dto"
/**
* Post process DTO files generated by OpenAPI generator.
* We need to replace `kotlin.collections.Map<kotlin.String, kotlin.Any>` with `kotlinx.serialization.json.JsonObject`
* due to missing serialization for kotlin.Any.
*/
tasks.register("postProcessDto") {
group = "openapi tools"
fileTree(dtoPath)
.matching {
include("**/*.kt")
}.forEach { file ->
if (file.isFile) {
val content = file.readText()
val updatedContent = content.replace(
"kotlin.collections.Map<kotlin.String, kotlin.Any>",
"kotlinx.serialization.json.JsonObject",
)
file.writeText(updatedContent)
}
}
}
Should do the same (except replacing Date, because we are using kotlinx.datetime)
Bug Report Checklist
Description
openapi-generator version
7.2.0
, don't know about previous versions as this was my first attemptOpenAPI declaration file content or url
With this schema :
I obtain this class:
Which causes an error on
val geoJson: kotlin.collections.Map<kotlin.String, kotlin.Any>
described as:Generation Details
Gradle plugin task:
Steps to reproduce
Execute the
openApiGenerate
taskRelated issues/PRs
Suggest a fix
Adding
@Contextual
to the second type ofMap
:kotlin.collections.Map<kotlin.String, @Contextual kotlin.Any>
seems to fix the issue.