Skip to content
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

Generate JSON Schema by Kotlin dataclass #8

Open
kry127 opened this issue May 1, 2022 · 3 comments
Open

Generate JSON Schema by Kotlin dataclass #8

kry127 opened this issue May 1, 2022 · 3 comments

Comments

@kry127
Copy link

kry127 commented May 1, 2022

Hello! I would like to suggest a feature.

As far as I know, there is a feature as mapping of Kotlin Dataclasses to JSON representation. Klaxon library is able to serialize arbitrary classes.
Is there any plans to support some kind of generation of schema by Kotlin Class/Dataclass definition?

https://stackoverflow.com/questions/60147305/how-to-generate-json-schema-from-kotlin-class

@pwall567
Copy link
Owner

pwall567 commented May 2, 2022

Hi – thanks for your suggestion.

You seem to be talking about two different issues. Yes, the Klaxon library is capable of serializing arbitrary classes, but so are several other libraries. I recommend kjson, but there are several others, including Java libraries with a Kotlin veneer.

As to generating JSON Schema files from Kotlin data classes, I have given it some thought but I haven't seen a great deal of demand. I have a code generator – json-kotlin-schema-codegen – which performs the reverse process, converting JSON Schema to Kotlin, and that is the way I am seeing more systems developed: start with a schema (or OpenAPI file containing multiple schema definitions) and generate data classes from that.

The JSON Schema official implementations page lists some schema generators from code, but there are none listed as taking Kotlin. The Java implementations may work with Kotlin, but I haven't tried.

@kry127
Copy link
Author

kry127 commented May 3, 2022

Yes, thanks for familiar link about generators, didn't see that section on the page.

The use-case of requested feature is quite opposite and maybe I am wrong about it's usefullness. I am building some kind of plugin-based system. The development of plugin is language-agnostic by design. Plugin parameters are retrieved by the main service via REST or gRPC API in the form of JSON Schema in order to know how to provide valid parameters to the plugin in response.

As from the point of the view of independent developer of plugin, there is no need to pre-define JSON Schema: you just need data class that configures plugin. All you need to do is to generate schema of this data class and provide it to external system, and validate configuration passed by external system against data class specification.

@pwall567
Copy link
Owner

pwall567 commented May 4, 2022

I'm not sure that I understand your use case, but I don't think any of my projects will be of much use to you. You can probably derive a certain amount of information about the structure of an object from the data class (from source, from the class file or probably most easily using reflection on the class) but validations (minimum, maximum, maxLength etc.) would be much more difficult. It's not a problem I've put much effort into, and I don't think I can help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants