-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix C# GenericHost oneOf serialization for referenced models #24674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,7 +376,41 @@ | |
|
|
||
| {{/children}} | ||
| {{/discriminator}} | ||
| {{^model.discriminator}} | ||
| {{#composedSchemas}} | ||
| {{#oneOf}} | ||
| {{^vendorExtensions.x-duplicated-data-type}} | ||
| {{#isPrimitiveType}} | ||
| if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} != null) | ||
| { | ||
| JsonSerializer.Serialize(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}, jsonSerializerOptions); | ||
| return; | ||
| } | ||
|
|
||
| {{/isPrimitiveType}} | ||
| {{/vendorExtensions.x-duplicated-data-type}} | ||
| {{/oneOf}} | ||
| {{/composedSchemas}} | ||
| {{/model.discriminator}} | ||
| writer.WriteStartObject(); | ||
| {{^model.discriminator}} | ||
| {{#composedSchemas}} | ||
| {{#oneOf}} | ||
| {{^vendorExtensions.x-duplicated-data-type}} | ||
| {{#isModel}} | ||
| {{^isContainer}} | ||
| if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} != null) | ||
| { | ||
| {{baseType}}JsonConverter {{#lambda.camelcase_sanitize_param}}{{baseType}}JsonConverter{{/lambda.camelcase_sanitize_param}} = new {{baseType}}JsonConverter(); | ||
| {{#lambda.camelcase_sanitize_param}}{{baseType}}JsonConverter{{/lambda.camelcase_sanitize_param}}.WriteProperties(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}, jsonSerializerOptions); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The referenced oneOf model's properties are written directly into the parent's already-open JSON object (flattened), followed by the parent's own WriteProperties. If a oneOf child schema shares a JSON property name with a sibling property on the parent (or with the discriminator/additionalProperties), the serialized output will contain duplicate keys for that name. The included Fruit sample avoids this only by luck (child keys kind/count/sweet vs parent key color); a child such as Prompt for AI agents |
||
| } | ||
|
|
||
| {{/isContainer}} | ||
| {{/isModel}} | ||
| {{/vendorExtensions.x-duplicated-data-type}} | ||
| {{/oneOf}} | ||
| {{/composedSchemas}} | ||
| {{/model.discriminator}} | ||
|
|
||
| {{#model.discriminator}} | ||
| {{#model.hasDiscriminatorWithNonEmptyMapping}} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: For primitive oneOf members the new code serializes the value as a bare JSON primitive and
returns beforewriter.WriteStartObject()is reached. This path is asymmetric with the referenced-object path it sits next to: the object path preserves the parent's sibling properties via WriteProperties, but the primitive path writes only the bare scalar and drops every sibling property (and never emits an object). For a composed model that mixes a primitive oneOf member with sibling properties (e.g.oneOf: [string, Apple]plus acolorsibling), serializing when the primitive is selected silently omits the sibling data, so the output won't round-trip. Consider writing the primitive into the object (or documenting that a primitive oneOf cannot be combined with siblings).Prompt for AI agents