Skip to content

Conversation

@kdallmeyer-sr
Copy link

In json-schema, when a field is optional, it does not need to be added to the "required" field. Unfortunately the Avro -> Json-Schema converter currently makes every field required in the resulting schema. This means that in the JSON every field must be present and either set to null or populated. In reality we should mark JSON with optional fields removed as valid as well.

Demo Avro Schema

{
  "type" : "record",
  "name" : "Demo",
  "namespace" : "com.shoprunner.data.demo",
  "fields" : [ {
    "name" : "name",
    "type" : "string"
  }, {
    "name" : "count",
    "type" : [ "null", "int" ]
  }, {
    "name" : "price",
    "type" : [ "null", "float" ]
  } ]
}

Converted JSON schema with required optional fields that nullable.

{
  "definitions" : {
    "record:com.shoprunner.data.demo.Demo" : {
      "type" : "object",
      "required" : [ "name", "count", "price" ],
      "additionalProperties" : false,
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "count" : {
          "oneOf" : [ {
            "type" : "null"
          }, {
            "type" : "integer",
            "minimum" : -2147483648,
            "maximum" : 2147483647
          } ]
        },
        "price" : {
          "oneOf" : [ {
            "type" : "null"
          }, {
            "type" : "number"
          } ]
        }
      }
    }
  },
  "$ref" : "#/definitions/record:com.shoprunner.data.demo.Demo"
}

JSON that is correctly validated with this schema

{
  "name" : "Bob",
  "count" : 1,
  "price" : 1.10
}

{
  "name" : "Bob",
  "count" : null,
  "price" : null
}

JSON that is incorrectly failed with this schema

{
  "name" : "Bob"
}

The change I am proposing will make the schema only require name:

{
  "definitions" : {
    "record:com.shoprunner.data.demo.Demo" : {
      "type" : "object",
      "required" : [ "name" ],
      "additionalProperties" : false,
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "count" : {
          "oneOf" : [ {
            "type" : "null"
          }, {
            "type" : "integer",
            "minimum" : -2147483648,
            "maximum" : 2147483647
          } ]
        },
        "price" : {
          "oneOf" : [ {
            "type" : "null"
          }, {
            "type" : "number"
          } ]
        }
      }
    }
  },
  "$ref" : "#/definitions/record:com.shoprunner.data.demo.Demo"
}

snagafritz added a commit to Snagajob/json-schema-avro that referenced this pull request Mar 16, 2018
…, should not be included in required properties of JSON schema

Combined fixes in the following PRs from the forked repository:
fge#2
fge#3
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

Successfully merging this pull request may close these issues.

1 participant