Comments (9)
This does not seem to be a bug.
Note that, as per documentation, JsonSchema.Net.Generation translates a member with an [obsolete]
attribute into a schema that is deprecated (e.g., a schema that has its "deprecated" keyword being set to true). In other words, the [obsolete]
does not instruct the json schema generator to ignore the respective member, contrary to your assumption.
As the documentation i linked to explains, if you want the schema generator to exclude/ignore the properties you marked as obsolete you should use either the [JsonIgnore]
attribute from STJ or JsonSchema.Net.Generation's attribute [JsonExclude]
. Note that the STJ attribute obviously has an effect on the STJ (de)serializer, whereas attributes from JsonSchema.Net.Generation won't affect the STJ (de)serializer.
Side note: schema.ToString()
isn't doing what you seem to believe it does (since JsonSchema doesn't override the ToString method, you are basically invoking the object.ToString implementation here). To get the json text of the schema, serialize the schema, e.g. string jsonSchemaText = JsonSerializer.Serialize(schema)
.
from json-everything.
Thank you
Starting with ToString()
, I almost line for line copied my Json.Net schema generation code, including ToString()
, and never got that far to discover the issue ;)
I do want old config to be read but not written, so as you state [JsonIgnore]
would not be the right choice.
If [Obsolete]
is translated into deprecated
, and the code seems valid for read and write, is the duplicate key exception really "not a bug", or is there no schema that can represent my code with all versions, including deprecated
, accounted for?
Other than adding JsonSchema.Net.Generation.JsonExclude]
to all the [Obsolete]
items, is there no way to dynamically filter the schema generation, e.g. as I do for writing in ExcludeObsoleteProperties()
?
from json-everything.
If
[Obsolete]
is translated intodeprecated
, and the code seems valid for read and write, is the duplicate key exception really "not a bug", or is there no schema that can represent my code with all versions, includingdeprecated
, accounted for?
You should be able to represent this situation in a json schema using the anyOf
composition keyword for the schema associated with the Child property. As far as i can tell from the documentation and perusing the public APIs of JsonSchema.Net.Generation, it seems the generator is not able to do this on its own. I'd suggest to manually improve the schema in this way after schema generation. Unless you are generating huge (amounts of) schemas, this shouldn't be too burdensome, especially since it is recommended to do a manual review of the generated schema(s) anyways, as stated by the documentation:
Ideally, this functionality should be used to create a starting point in authoring a schema. The schemas output by this library should be reviewed by actual people prior to being put into a production system.
I am not aware of other quick and easy ways to do this, unfortunately, and i'd like to defer to the author/maintainer of JsonSchema.Net.Generation with respect to advising what is the best way to address this scenario.
That said, i know of two ways to technically address your issue, but they don't seem appealing to me for different reasons.
There is the possibility of telling the schema generator your intents (https://docs.json-everything.net/schema/schemagen/schema-generation/#schema-schemagen-extension) regarding the usage of anyOf
. But you would still need to implement some logic that detects hidden properties/fields in the types for which are schemas generated and then composes AnyOfIntent
instances accordingly. The simplest place for doing that would theoretically be a refiner (see the aforementioned documentation link), but you would then still need to avoid/suppress the exception. Which would leave you with the unfortunate situation that you would still need to use for example the [JsonExclude]
attribute, which kinda reads opposite to what the desired outcome will be.
The second way i know of that would also avoid needing [JsonExclude]
annotations would be to implement and register your own schema generator based on the ISchemaGenerator
interface. But that's costly, because you can't simply derive from and override the behavior of the default ObjectSchemaGenerator (because it is an internal type and neither has overridable methods) and thus would require you to duplicate a significant amount of logic already implemented in ObjectSchemaGenerator. Which also to a large degree nullifies the convenience of using the JsonSchema.Net.Generation if you choose to do that. So, basically, i wouldn't recommend doing that either, because for this effort to be justified you would need to generate schemas for a metric ton of types with a butt load of hidden members you want to be accounted for in the schemas.
from json-everything.
Agree, none of those options are appealing.
In Newtonsoft.Schema I used internal get
to suppress schema generation for [Obsolete]
items, also not pretty, and internal get
does not play nice with Text.Json.
Maybe naively, it seems a method similar to Text.Json TypeInfoResolver = new DefaultJsonTypeInfoResolver() .WithAddedModifier(ExcludeObsoleteProperties)
could allow me to control the behavior, or maybe much simpler a configuration option on excluding [Obsolete]
items from schema generation.
from json-everything.
I agree with the refiner approach. In the refiner, you'd want to check for a PropertiesIntent
that has any entries with a DeprecatedIntent
keyword, and then remove those entries.
from json-everything.
Great, any pointers to an API or example that shows the use?
from json-everything.
(Sorry about the previous comment. Getting issues confused.)
Refiners and everything else is in the docs, which has links in all the right places.
from json-everything.
Thank you, will investigate and report back.
from json-everything.
A custom refiner looked like it would take me more time than I was willing to spend on this, so I'm just using [JsonSchema.Net.Generation.JsonExclude]
and [obsolete]
.
As an enhancement a general option to exclude[obsolete]
items could be added, or an easier way to customize behavior similar to e.g. WithAddedModifier()
.
from json-everything.
Related Issues (20)
- Extract data annotation attributes from JsonSchema.Net.Generation into separate project HOT 2
- Is there a way to do JSON validation that does case insensitive matching on property names? HOT 8
- Support language packs in schema playground
- sileo-featured.json
- How to export or save JsonSchema to plaintext? HOT 4
- How to write a JsonSchemaBuilder for a class that has a property of a class? HOT 4
- Schema generation doesn't work on site HOT 1
- maximum/minimum use decimal which do not parse certain many double "number" strings HOT 3
- `Json.Schema.JsonSchema.Evaluate` throws "`System.OverflowException`: Value was either too large or too small for a Decimal." HOT 4
- Get json data from only required properties based on the attributes set HOT 4
- Schema validation crashes with stack overflow HOT 12
- Patch Path Not Escaping Forward Slash HOT 6
- EvaluationResults default value shows up in Annotations when number out or range, but not on other situations HOT 3
- Performance vs Newtonsoft.Json.Schema HOT 6
- Pattern and PatternProperties keyword performance and security improvements HOT 3
- How to add a keyword before another one HOT 2
- Schema validation doesn't fill EvaluationResults with any Errors for anyOf HOT 3
- ValidateMetaSchema option not working
- Why is NullableAttribute needed for nullable properties HOT 3
- Peformance issue with certain type of schema HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.