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

Handle optional fields properly by not adding it to 'required' elements #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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