creek-service/creek-json-schema-gradle-plugin

Marking String fields as nullable

rafal-gss opened this issue · 11 comments

Is your feature request related to a problem? Please describe.
By default, a class that has a property of type String will generate a schema with type: string, which does not accept nulls.
What we need instead is ["string", "null"]

Describe the solution you'd like
Add an annotation that would allow defining whether given String field in nullable or not.

Describe alternatives you've considered

Additional context

Hi @rafal-gss, that's by design. Creek avoids nulls as much as possible.

If a property is optional, use Optional<String>. Then the JSON document won't include the property if it's not present.

You may also be able to combine Optional<String> with the @JsonInclude attribute to always included it. I think that should then allow nulls in the document. But I'm not near my laptop to check.

Actually, I don't think @JsonInclude will work.

However, you can always inject data into the schema with @JsonSchemaInject annotation on the getter. e.g. something like:

class Model {
   @JsonSchemaInject(json = "{\"type\": [\"null\", \"string\"]}")
   public Optional<String> getFoo() {
        return s;
    }

    //...
}

The above would give you a JSON schema containing:

"properties": {
   "foo": { "type": ["null", "string"] }
 }

Though I'd still encourage you to avoid null values in your JSON documents, just like I'd encourage you to avoid using nulls in your code APIs.

Let me know how you get on.

Hi @big-andy-coates, thanks for a quick response!

Looks like the @JsonSchemaInject annotation will do the trick for us :)
I'll give it a go on our Java records and let you know if it worked.

No problem @rafal-gss

Out of interest, are you looking at creek in general, or just needed a schema generator plugin?

Just a schema generator.
I have a pretty large set of existing models here and need to generate schemas for them.
Hence moving to Option<String> would be a pretty costly solution :)

@big-andy-coates looks like the annotation you've mentioned is not part of the 'org.creekservice:creek-base-annotation:0.4.1' library. Could you point me to the right direction?

Ugh, looks like it's abandoned. :/
We won't be able to use this lib due to its vulnerabilities.

I think it would be great if the creek libraries were more 'self-contained' - allowing for its usage without importing other libraries.

For now, we're gonna have to go with another approach.
Thanks for your help.

Turns out the Optional<String> approach doesn't work either, the generated schema for the field has type: string

Ugh, looks like it's abandoned. :/
We won't be able to use this lib due to its vulnerabilities.

Yep, I hear you: creek-service/creek-json-schema#272

However, you can manually update dependencies with vulnerabilities in your own build files to ensure you're working with versions that have no known vulnerabilities.

Good luck either way :)

Cheers, Andy!