fsprojects / FSharp.Data.JsonSchema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi Case DUs should set 'additionalProperties' to false

blakeSaucier opened this issue · comments

commented

Hello,

Normally when generating a jsonSchema for a type, the properties are listed along with the additionalProperties flag set to false. As an example:

type TestRecord = { FirstName: string; LastName: string }

will generate to:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "TestRecord",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    }
  }
}

Notice that the "additionalProperties": false. In addition, single case DUs also have this behaviour.

When generating a schema for a multi case DU:

type MultiCase =
| WithOneField of int
| WithNamedFields of name: string * value: float
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "TestDU",
  "definitions": {
    "WithOneField": {
      "type": "object",
      "required": [
        "tag",
        "item"
      ],
      "properties": {
        "tag": {
          "type": "string",
          "default": "WithOneField",
          "x-enumNames": ["WithOneField"],
          "enum": ["WithOneField"]
        },
        "item": {
          "type": "integer"
        }
      }
    },
    "WithNamedFields": {
      "type": "object",
      "required": [
        "tag",
        "name",
        "value"
      ],
      "properties": {
        "tag": {
          "type": "string",
          "default": "WithNamedFields",
          "x-enumNames": ["WithNamedFields"],
          "enum": ["WithNamedFields"]
        },
        "name": {
          "type": "string"
        },
        "value": {
          "type": "number"
        }
      }
    }
  },
  "anyOf": [
    {
      "$ref": "#/definitions/WithOneField"
    },
    {
      "$ref": "#/definitions/WithNamedFields"
    }
  ]
}

Notice that the additionalProperties flag is missing.

This has downstream implications for validating json against this schema as well as generating models in other languages from this schema. I can try and put together a PR.