awslabs / aws-glue-schema-registry

AWS Glue Schema Registry Client library provides serializers / de-serializers for applications to integrate with AWS Glue Schema Registry Service. The library currently supports Avro, JSON and Protobuf data formats. See https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html to get started.
Apache License 2.0
131 stars 97 forks source link

Support for nullable fields in JsonSchema generation from POJO's #73

Open johancm opened 3 years ago

johancm commented 3 years ago

When sending POJO's using the JSON data format, the schema that gets generated do not allow for nullable fields. There is also no way to pass this intent as part of the configuration.

In particular, I'm referring to the following: com.amazonaws.services.schemaregistry.serializers.json.JsonSerializer

serializes using the default configuration

this.jsonSchemaGenerator = new JsonSchemaGenerator(this.objectMapper);

However, the mbknor-jackson-jsonSchema library used has a configuration that, when generating a schema allows for nullable files , see the Nullable types section here

JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
JsonSchemaGenerator generator = new JsonSchemaGenerator(objectMapper, config);

This library needs to be extended with a configuration to allow for the generation of a schema that supports nullable types as described above.

without nullable fileds

"properties": {
        "addressId": {
          "type": "string"
        },

vs

with nullable fields

"properties": {
        "addressId": {
          "oneOf": [
              {
              "type": "null",
              "title": "Not included"
              },
              {
                "type": "string"
              }
          ]
        },
hhkkxxx133 commented 3 years ago

Hello,

Thanks for reaching out to us. I think it would make sense to add this configuration into GlueSchemaRegistryConfiguration that will be consumed in the JsonSerializer. By default it will be disabled.

We are happy to accept a PR if you can raise one. It would be great if you can include a test. Thanks!