Replies: 2 comments 1 reply
-
Just a FYI to clarify a false assumption, my post is however not touching upon your issue.
To the contrary; the current JsonSchema specification explicitly calls out that other properties are allowed next to a
The JsonSchema spec also features an example that shows a property next to the Older JsonSchema drafts/specs stipulated that other properties next to
No, because other properties next to a
Not sure what is going on there on your end, but a quick check using the Newtonsoft JSON Schema online validator shows that Newtonsoft JSON Schema apparently consumes your example schema without any problems: |
Beta Was this translation helpful? Give feedback.
-
Yes, because the shown code - not STJ - explicitly adds a "description" property to the
I would use
That's not for a simply illustrative code example in the documentation to deal with. This is getting too complicated for a code example that is meant to illustrate and educate about the Ideally, the STJ schema generator could make many of such complications go away by not embedding referenced schemas as/into other property schemas. Similar to what JsonSchema.Net.Generation does, the STJ schema generator would have to separate schemas generated from a type from the schemas assigned to properties. The "type schemas" would be bundled in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to avoid generating schemas via System.Text.Json with duplicate custom attributes when referenced subschemas are created (the attribute is already declared in the referenced schema, but using the Microsoft example on how to add custom attributes, the attribute gets declared a second time within the element using the reference).
Following the example provided at
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/extract-schema#transform-the-generated-schema
to transform a generated schema to support the System.ComponentModel.DescriptionAttribute causes the "description" attribute to be listed twice when a generated schema results in use of references (via $ref).
In the example below, I start with the exact code from the link provided above, only I change the
Person.Name
property from a simple string to be an object that contains two child strings (First
andLast
). Then instead of generating a schema for thePerson
object, I generate a schema for an object containing two differentPerson
objects so that the generated schema simply makes the second Person reference the first person's schema:The resulting schema now technically lists "description" twice for Person2.Name. Once directly inside Person2.Name's schema, then again indirectly via the referenced schema:
"$ref": "#/properties/Person1/properties/Name"
.My current solution is to change the TransformSchemaNode code to check if the current schema contains a child named "$ref" before choosing to insert "description":
But I'm not sure if this is the correct/best/safest way to accomplish this. For example, this wouldn't work quite right if the current schema generation logic in STJ might point a property that has been attributed with a specific [Description] attribute to reference a different property (that has the same type) but a different [Description] value? In such a case the schema would probably need to be duplicated rather than referenced.
I actually encountered the issue because I am using
TransformSchemaNode
to add support for adding a"title"
to properties using theDisplayNameAttribute
. Then other software that happens to be using the Newtonsoft JSON Schema library fail to load any properties that have this extra attribute defined. I suspect the root of the issue is that schemas probably aren't supposed to list any sibling attributes to a"$ref"
(all information should be contained within the referenced schema).Regardless whether my solution is the best way to currently handle this or there is a better solution, I wonder if the example documentation from Microsoft (linked above) should be updated to show how to avoid this situation as it isn't an obvious problem and everything seems fine at first.
Beta Was this translation helpful? Give feedback.
All reactions