Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = {{#vendorExtensions.x-discriminator-visible-true}}true{{/vendorExtensions.x-discriminator-visible-true}}{{^vendorExtensions.x-discriminator-visible-true}}false{{/vendorExtensions.x-discriminator-visible-true}})
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes(
{{#discriminator.mappedModels}}
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = {{modelName}}::class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{^-last}},{{/-last}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,17 @@ public void oneOfWithDiscriminator_generatesSealedClassWithDiscriminatorProperty
"sealed class Pet(",
"open val petType: kotlin.String",
"@com.fasterxml.jackson.annotation.JsonTypeInfo",
// every mapped child model declares/inherits the discriminator property, so
// Jackson must reuse it instead of injecting a second copy (duplicated key)
"include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY",
"property = \"petType\"",
"visible = true",
"@com.fasterxml.jackson.annotation.JsonSubTypes"
);
assertFileNotContains(
petModel,
"include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,"
);
}

@Test
Expand Down Expand Up @@ -571,12 +578,15 @@ public void fixJacksonJsonTypeInfoInheritance_canBeDisabled() throws IOException
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petModel = Paths.get(outputPath + "/models/Pet.kt");

// When fixJacksonJsonTypeInfoInheritance is false and parent has no properties,
// visible should be false for oneOf pattern
// With As.EXISTING_PROPERTY the discriminator is read from/written to the real
// property, which every mapped child model declares or inherits; visible=true
// keeps that property populated on deserialization so round-trips preserve it
assertFileContains(
petModel,
"visible = false"
"include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY",
"visible = true"
);
assertFileNotContains(petModel, "visible = false");
}

// ==================== useTags for JAXRS-SPEC ====================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package org.openapitools.server.models
* @param name
* @param petType
*/
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY, property = "petType", visible = true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: As.EXISTING_PROPERTY now makes serialization depend on the caller-supplied petType, but these allOf child models accept any string rather than defaulting or constraining it to Cat/Dog. A Cat with another value emits an unmappable discriminator and cannot round-trip through Pet; generate subtype-specific defaults/constants for these children or otherwise ensure the existing property always contains the mapped subtype value before using this mode.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At samples/server/others/kotlin-server/polymorphism-allof-and-discriminator/src/main/kotlin/org/openapitools/server/models/Pet.kt, line 20:

<comment>`As.EXISTING_PROPERTY` now makes serialization depend on the caller-supplied `petType`, but these allOf child models accept any string rather than defaulting or constraining it to `Cat`/`Dog`. A `Cat` with another value emits an unmappable discriminator and cannot round-trip through `Pet`; generate subtype-specific defaults/constants for these children or otherwise ensure the existing property always contains the mapped subtype value before using this mode.</comment>

<file context>
@@ -17,7 +17,7 @@ package org.openapitools.server.models
  * @param petType 
  */
-@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
+@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY, property = "petType", visible = true)
 @com.fasterxml.jackson.annotation.JsonSubTypes(
     com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Cat::class, name = "Cat"),
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same fix as above thread — see the reply there (fde104a makes the property authoritative via visible=true).

@com.fasterxml.jackson.annotation.JsonSubTypes(
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Cat::class, name = "Cat"),
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Dog::class, name = "Dog")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.openapitools.server.models.Dog
/**
* A pet
*/
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "petType", visible = false)
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY, property = "petType", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes(
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Cat::class, name = "cat"),
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Dog::class, name = "dog")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.openapitools.server.models.Dog
* A pet
* @param petType
*/
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY, property = "petType", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes(
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Cat::class, name = "cat"),
com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = Dog::class, name = "dog")
Expand Down
Loading