Git Product home page Git Product logo

Comments (9)

elgonzo avatar elgonzo commented on July 18, 2024

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.

ptr727 avatar ptr727 commented on July 18, 2024

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.

elgonzo avatar elgonzo commented on July 18, 2024

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?

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.

ptr727 avatar ptr727 commented on July 18, 2024

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.

gregsdennis avatar gregsdennis commented on July 18, 2024

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.

ptr727 avatar ptr727 commented on July 18, 2024

Great, any pointers to an API or example that shows the use?

from json-everything.

gregsdennis avatar gregsdennis commented on July 18, 2024

(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.

ptr727 avatar ptr727 commented on July 18, 2024

Thank you, will investigate and report back.

from json-everything.

ptr727 avatar ptr727 commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.