Coder Social home page Coder Social logo

Comments (2)

ahl avatar ahl commented on June 12, 2024

I like this idea of (roughly) JsonSchema::json_schema(..) -> serde_json::Value a lot. At first, I thought it was weird and lame, but the more I thought it over, the more it appeals to me. Consider my own use case: we're using schemars for a bunch of OpenAPI related stuff. We have to specify that we're interested in the OpenAPI format and fish around the extensions for nullable. It would be much crisper to just get back a json Value and use that as is.

Your insight that this divorces the output type from the weird differences of various JSON schema revisions is extremely compelling. This even opens the door for a generic "schema" representation that can have a JsonSchema impl (e.g. https://crates.io/crates/schema and there could be a impl<T:schema::Schema> schemars::JsonSchema for T {}).

One could imagine schema descriptions even unrelated to JSON schema... though perhaps that's a bridge too far.

In addition, I think it makes a ton of sense to have the structural use of JSON schema types live in a different crate -- the design goals are distinct and not necessarily well-aligned.

One question: do you definitely want to use serde_json::Value? On one hand, consumers of schemars almost certainly also depend on serde_json. On the other hand, I've often wished there were a distinct Value object so I didn't need to pull in all of serde_json.


This does seem to complicate hand-written JsonSchema implementations in that one might need to pay closer attention to the generation settings. It might be useful to provide some mechanism that's effectively "here's my output in 2019-09" format, could you please transform this according to what the caller is asking for?"


Cool stuff; would be happy to contribute; I've already been planning a 2020-12 JSON Schema crate for an OpenAPI v3.1 compatible version of https://crates.io/crates/openapiv3

from schemars.

gagbo avatar gagbo commented on June 12, 2024

Hello,

The changes all make sense, as my main (only) use case for the library is to produce programmatically the #/components/schemas part of an OpenAPI schema from a collection of Rust structs, I’m very happy to see it’s going to provide some extra flexibility in usage.

For the ability to produce extra schema outputs, I feel like that having the option to specify in the json_schema call which variant to target is going to be the simplest thing to do. For all people that don’t know/care about the version, we could have both json_schema and json_schema_with_spec or something.

I’d like the ability to also output the data to yaml format if possible, it would be useful at least for my use case I think. But the more I think about it, the more I think that I might just want a macro that creates an openapiv3::Schema from arbitrary structs, so maybe all of this is irrelevant and I should just look into the code that respects serde attributes here.

I think all the specific getters/setters like format() set_format would fit better if they were added to extension traits like trait JsonSchema202012 which you’d implement on Schema. This way consumers that want to use these would need to bring the trait in scope and it would make completion/method list smaller. Also it would allow external crates to implement their own extension traits if they want to, I think.

from schemars.

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.