@@ -126,24 +126,38 @@ case ValuesSchema(var valueSchema) -> JsonObject.of(Map.of(
126126 "key2" , buildCompliantJtdDocument (valueSchema )
127127 ));
128128 case DiscriminatorSchema (var discriminator , var mapping ) -> {
129- final var firstEntry = mapping .entrySet ().iterator ().next ();
130- final var discriminatorValue = firstEntry .getKey ();
131- final var variantSchema = firstEntry .getValue ();
132-
133- // Discriminator schemas always generate objects with the discriminator field
134- final var members = new LinkedHashMap <String , JsonValue >();
135- members .put (discriminator , JsonString .of (discriminatorValue ));
136-
137- // Add properties based on the variant schema type
138- if (variantSchema instanceof PropertiesSchema props ) {
139- props .properties ().forEach ((key , valueSchema ) ->
140- members .put (key , buildCompliantJtdDocument (valueSchema ))
141- );
142- }
143- // For TypeSchema variants, the object with just the discriminator field should be valid
144- // For EnumSchema variants, same logic applies
145-
146- yield JsonObject .of (members );
129+ final var firstEntry = mapping .entrySet ().iterator ().next ();
130+ final var discriminatorValue = firstEntry .getKey ();
131+ final var variantSchema = firstEntry .getValue ();
132+
133+ // Discriminator schemas always generate objects with the discriminator field
134+ final var members = new LinkedHashMap <String , JsonValue >();
135+ // ======================== CHANGE: FIX DISCRIMINATOR VALUE GENERATION ========================
136+ // WRONG: members.put(discriminator, buildCompliantJtdDocument(valueSchema)); // generates random values
137+ // CORRECT: Use the discriminator mapping key as the value
138+ members .put (discriminator , JsonString .of (discriminatorValue ));
139+ // ==================== END CHANGE: FIX DISCRIMINATOR VALUE GENERATION ====================
140+
141+ // Add properties based on the variant schema type
142+ if (variantSchema instanceof PropertiesSchema props ) {
143+ // ======================== CHANGE: SKIP DISCRIMINATOR FIELD IN PROPERTIES ========================
144+ // Don't re-add the discriminator field when processing properties
145+ props .properties ().forEach ((key , valueSchema ) -> {
146+ if (!key .equals (discriminator )) { // Skip discriminator field to avoid overwriting
147+ members .put (key , buildCompliantJtdDocument (valueSchema ));
148+ }
149+ });
150+ props .optionalProperties ().forEach ((key , valueSchema ) -> {
151+ if (!key .equals (discriminator )) { // Skip discriminator field to avoid overwriting
152+ members .put (key , buildCompliantJtdDocument (valueSchema ));
153+ }
154+ });
155+ // ==================== END CHANGE: SKIP DISCRIMINATOR FIELD IN PROPERTIES ====================
156+ }
157+ // For TypeSchema variants, the object with just the discriminator field should be valid
158+ // For EnumSchema variants, same logic applies
159+
160+ yield JsonObject .of (members );
147161 }
148162 case NullableSchema (var inner ) -> JsonNull .of ();
149163 };
0 commit comments