Skip to content

Commit 72898fc

Browse files
committed
[Bugfix] Adjust $ref handling logic in DefaultJsonSchemaFactory to avoid replacing nested type name if it shares the prefix of the root class name
When you have a type that shares the name of it's parent type (eg: Task & TaskType), with the way the current `fixRefsPrefix` works (recurses through all subtypes to handle nested types) you end up replacing the def path of the subtype and it becomes unserialisable and crashes at runtime, and you have to use custom serialisers or rename your class. This checks that the root definition is followed by a / (to indicate nesting) before replacing it).
1 parent 79bb843 commit 72898fc

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

sdk-serde-kotlinx/src/main/kotlin/dev/restate/serde/kotlinx/DefaultJsonSchemaFactory.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import io.github.smiley4.schemakenerator.jsonschema.JsonSchemaSteps.generateJson
1515
import io.github.smiley4.schemakenerator.jsonschema.TitleBuilder
1616
import io.github.smiley4.schemakenerator.jsonschema.data.IntermediateJsonSchemaData
1717
import io.github.smiley4.schemakenerator.jsonschema.data.RefType
18-
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.*
18+
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonArray
19+
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonNode
20+
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonObject
21+
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.JsonTextValue
22+
import io.github.smiley4.schemakenerator.jsonschema.jsonDsl.array
1923
import io.github.smiley4.schemakenerator.serialization.SerializationSteps.analyzeTypeUsingKotlinxSerialization
2024
import io.github.smiley4.schemakenerator.serialization.SerializationSteps.initial
2125
import io.github.smiley4.schemakenerator.serialization.SerializationSteps.renameMembers
@@ -62,7 +66,7 @@ object DefaultJsonSchemaFactory : KotlinSerializationSerdeFactory.JsonSchemaFact
6266
// Add $schema
6367
rootNode.properties.put(
6468
"\$schema",
65-
JsonTextValue("https://json-schema.org/draft/2020-12/schema"),
69+
JsonTextValue("https://json-schema.org/draft/2020-12/schema"),
6670
)
6771
// Add $defs
6872
val definitions =
@@ -104,7 +108,7 @@ object DefaultJsonSchemaFactory : KotlinSerializationSerdeFactory.JsonSchemaFact
104108
(schema.json as JsonObject).properties["title"] == null
105109
) {
106110
(schema.json as JsonObject).properties["title"] =
107-
JsonTextValue(TitleBuilder.BUILDER_SIMPLE(schema.typeData, this.typeDataById))
111+
JsonTextValue(TitleBuilder.BUILDER_SIMPLE(schema.typeData, this.typeDataById))
108112
}
109113
}
110114
}

0 commit comments

Comments
 (0)