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

Question: How to create json schema from a case class, and create custom schemas for specific types #305

Open
hagay3 opened this issue Jul 5, 2023 · 2 comments
Assignees

Comments

@hagay3
Copy link

hagay3 commented Jul 5, 2023

Hi,
First of all, thank you for the great library !!

  1. I have a specific use case in which I want to create a schema for a case class in which most of the types are known and can be created automatically, for example: String, Int , etc.
    But for some other types I want to create a custom schema manually, how can I achieve that?

  2. In addition I want to create a custom schema and change the field name, for example:
    case class CusotmClass(unsafeMap: Map[String, Int])
    in the json the case class will actually look like the following, instead of "unsafeMap" it will be written "flags".

"flags" : {
"a1" : 1,
"a2" : 2
}

  1. How can I create custom scehma for FiniteDuration ?

Thanks :)

@andyglow
Copy link
Owner

hello @hagay3
would be much easier if you'd send me a link to your https://scastie.scala-lang.org/ playground with classes/types you want to cover

but in general for case 1 what you need to do is make sure that manually created schemas for your types have been put into an implicit scope when the json schema for the main case class is getting generated

for the 2: there is no way to make code generator generate a json schema model containing renamed field, but you can update the model on your own

say

case class Foo(foo: String)
val fooSchema = Json.objectSchema[Foo].withFieldsUpdated { 
  case f if f.name == "foo" => f.copy(name = "bar")
}

for the 3 i'd recommend to take a quick look at https://github.com/andyglow/scala-jsonschema/blob/master/modules/joda-time/src/main/scala/com/github/andyglow/jsonschema/JodaTimeSupport.scala

cheers

@hagay3
Copy link
Author

hagay3 commented Jul 29, 2023

@andyglow
Thank you for your kind help, the details were pretty helpful!

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

No branches or pull requests

2 participants