我的 build.gradle.kts 文件中有此配置
tasks.register<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("openApiGenerateSamApi") {
generatorName.set("kotlin")
inputSpec.set("src/main/resources/sam-1.0-patched.yml")
outputDir.set(samOpenApiGenerateOutputDir)
modelPackage.set("ch.srg.sam.generated.model")
modelNameSuffix.set("Dto")
generateModelDocumentation.set(false)
configOptions.set(
mapOf(
Pair("dateLibrary", "java8"),
Pair("serializationLibrary", "jackson"),
Pair("enumPropertyNaming", "UPPERCASE"),
Pair("failOnUnknownProperties", "false"),
),
)
}
tasks.getByName("openApiGenerateSamApi").dependsOn("kspKotlin")
但是生成的序列化程序不包含 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES:
object Serializer {
@JvmStatic
val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
.findAndRegisterModules()
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
}
即使我将failOnUnknownProperties设置为true也没有效果。 问题是,一旦服务将字段添加到其域模型中,客户端就毫无用处
最后我只使用标准的序列化库,即GSON。有了它,就不存在未知属性的问题。
configOptions.set(
mapOf(
Pair("dateLibrary", "java8"),
Pair("enumPropertyNaming", "UPPERCASE"),
),
)