Set additionalProperties to false on enums where appropriate

This commit is contained in:
Graham Esau 2021-03-20 18:45:29 +00:00
parent 3a7d7ad905
commit 6a3bba1e86
12 changed files with 727 additions and 31 deletions

View file

@ -0,0 +1,106 @@
mod util;
use schemars::{JsonSchema, Map};
use util::*;
// Ensure that schemars_derive uses the full path to std::string::String
pub struct String;
#[derive(Debug, JsonSchema)]
pub struct UnitStruct;
#[derive(Debug, JsonSchema)]
pub struct Struct {
foo: i32,
bar: bool,
}
// Outer container should always have additionalPropreties: false
// `Struct` variant should have additionalPropreties: false
#[derive(Debug, JsonSchema)]
#[schemars(rename_all = "camelCase", deny_unknown_fields)]
pub enum External {
UnitOne,
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
foo: i32,
bar: bool,
},
UnitTwo,
Tuple(i32, bool),
#[schemars(with = "i32")]
WithInt,
}
#[test]
fn enum_external_tag() -> TestResult {
test_default_generated_schema::<External>("enum-external-duf")
}
// Only `Struct` variant should have additionalPropreties: false
#[derive(Debug, JsonSchema)]
#[schemars(tag = "typeProperty", deny_unknown_fields)]
pub enum Internal {
UnitOne,
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
foo: i32,
bar: bool,
},
UnitTwo,
#[schemars(with = "i32")]
WithInt,
}
#[test]
fn enum_internal_tag() -> TestResult {
test_default_generated_schema::<Internal>("enum-internal-duf")
}
// Only `Struct` variant should have additionalPropreties: false
#[derive(Debug, JsonSchema)]
#[schemars(untagged, deny_unknown_fields)]
pub enum Untagged {
UnitOne,
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
foo: i32,
bar: bool,
},
Tuple(i32, bool),
#[schemars(with = "i32")]
WithInt,
}
#[test]
fn enum_untagged() -> TestResult {
test_default_generated_schema::<Untagged>("enum-untagged-duf")
}
// Outer container and `Struct` variant should have additionalPropreties: false
#[derive(Debug, JsonSchema)]
#[schemars(tag = "t", content = "c", deny_unknown_fields)]
pub enum Adjacent {
UnitOne,
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
foo: i32,
bar: bool,
},
Tuple(i32, bool),
UnitTwo,
#[schemars(with = "i32")]
WithInt,
}
#[test]
fn enum_adjacent_tagged() -> TestResult {
test_default_generated_schema::<Adjacent>("enum-adjacent-tagged-duf")
}

View file

@ -34,7 +34,8 @@
}
}
}
}
},
"additionalProperties": false
}
]
}

View file

@ -31,7 +31,8 @@
}
}
}
}
},
"additionalProperties": false
}
]
}

View file

@ -0,0 +1,200 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Adjacent",
"anyOf": [
{
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"UnitOne"
]
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"StringMap"
]
},
"c": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"UnitStructNewType"
]
},
"c": {
"$ref": "#/definitions/UnitStruct"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"StructNewType"
]
},
"c": {
"$ref": "#/definitions/Struct"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Struct"
]
},
"c": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Tuple"
]
},
"c": {
"type": "array",
"items": [
{
"type": "integer",
"format": "int32"
},
{
"type": "boolean"
}
],
"maxItems": 2,
"minItems": 2
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"UnitTwo"
]
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"WithInt"
]
},
"c": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
}
}
}

View file

@ -0,0 +1,135 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "External",
"anyOf": [
{
"type": "string",
"enum": [
"unitOne",
"unitTwo"
]
},
{
"type": "object",
"required": [
"stringMap"
],
"properties": {
"stringMap": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"unitStructNewType"
],
"properties": {
"unitStructNewType": {
"$ref": "#/definitions/UnitStruct"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"structNewType"
],
"properties": {
"structNewType": {
"$ref": "#/definitions/Struct"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"struct"
],
"properties": {
"struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"tuple"
],
"properties": {
"tuple": {
"type": "array",
"items": [
{
"type": "integer",
"format": "int32"
},
{
"type": "boolean"
}
],
"maxItems": 2,
"minItems": 2
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"withInt"
],
"properties": {
"withInt": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
}
}
}

View file

@ -21,7 +21,8 @@
"type": "string"
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -32,7 +33,8 @@
"unitStructNewType": {
"$ref": "#/definitions/UnitStruct"
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -43,7 +45,8 @@
"structNewType": {
"$ref": "#/definitions/Struct"
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -67,7 +70,8 @@
}
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -89,7 +93,8 @@
"maxItems": 2,
"minItems": 2
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -101,7 +106,8 @@
"type": "integer",
"format": "int32"
}
}
},
"additionalProperties": false
}
],
"definitions": {

View file

@ -0,0 +1,130 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Internal",
"anyOf": [
{
"type": "object",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"UnitOne"
]
}
}
},
{
"type": "object",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"StringMap"
]
}
},
"additionalProperties": {
"type": "string"
}
},
{
"type": "object",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"UnitStructNewType"
]
}
}
},
{
"type": "object",
"required": [
"bar",
"foo",
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
},
{
"type": "object",
"required": [
"bar",
"foo",
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
},
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"UnitTwo"
]
}
}
},
{
"type": [
"object",
"integer"
],
"format": "int32",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"WithInt"
]
}
}
}
]
}

View file

@ -0,0 +1,77 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Untagged",
"anyOf": [
{
"type": "null"
},
{
"type": "object",
"additionalProperties": {
"type": "string"
}
},
{
"$ref": "#/definitions/UnitStruct"
},
{
"$ref": "#/definitions/Struct"
},
{
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": false
},
{
"type": "array",
"items": [
{
"type": "integer",
"format": "int32"
},
{
"type": "boolean"
}
],
"maxItems": 2,
"minItems": 2
},
{
"type": "integer",
"format": "int32"
}
],
"definitions": {
"UnitStruct": {
"type": "null"
},
"Struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
}
}
}

View file

@ -19,7 +19,8 @@
}
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -30,7 +31,8 @@
"newType": {
"type": "boolean"
}
}
},
"additionalProperties": false
},
{
"type": "object",
@ -52,7 +54,8 @@
"maxItems": 2,
"minItems": 2
}
}
},
"additionalProperties": false
}
]
}

View file

@ -18,7 +18,8 @@
"type": "number",
"format": "float"
}
}
},
"additionalProperties": false
}
]
}