From e830c7f79815508f066725a579fbe00025f576bf Mon Sep 17 00:00:00 2001 From: omissis Date: Mon, 1 May 2023 16:39:34 +0200 Subject: [PATCH] chore: add regression tests for additionalProperties as highlighted in issue #32 and #51 --- tests/data/regressions/issue.32.go.output | 40 +++++++++++++++++++++++ tests/data/regressions/issue.32.json | 39 ++++++++++++++++++++++ tests/data/regressions/issue.51.go.output | 8 +++++ tests/data/regressions/issue.51.json | 10 ++++++ tests/integration_test.go | 6 ++++ 5 files changed, 103 insertions(+) create mode 100644 tests/data/regressions/issue.32.go.output create mode 100644 tests/data/regressions/issue.32.json create mode 100644 tests/data/regressions/issue.51.go.output create mode 100644 tests/data/regressions/issue.51.json diff --git a/tests/data/regressions/issue.32.go.output b/tests/data/regressions/issue.32.go.output new file mode 100644 index 00000000..6dc056be --- /dev/null +++ b/tests/data/regressions/issue.32.go.output @@ -0,0 +1,40 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package test + +import "encoding/json" +import "fmt" + +type TestObject struct { + // Config corresponds to the JSON schema field "config". + Config TestObjectConfig `json:"config,omitempty" yaml:"config,omitempty" mapstructure:"config,omitempty"` + + // Name corresponds to the JSON schema field "name". + Name string `json:"name" yaml:"name" mapstructure:"name"` + + // Owner corresponds to the JSON schema field "owner". + Owner string `json:"owner" yaml:"owner" mapstructure:"owner"` +} + +type TestObjectConfig map[string]interface{} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *TestObject) UnmarshalJSON(b []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + if v, ok := raw["name"]; !ok || v == nil { + return fmt.Errorf("field name in TestObject: required") + } + if v, ok := raw["owner"]; !ok || v == nil { + return fmt.Errorf("field owner in TestObject: required") + } + type Plain TestObject + var plain Plain + if err := json.Unmarshal(b, &plain); err != nil { + return err + } + *j = TestObject(plain) + return nil +} diff --git a/tests/data/regressions/issue.32.json b/tests/data/regressions/issue.32.json new file mode 100644 index 00000000..eede76bf --- /dev/null +++ b/tests/data/regressions/issue.32.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/TestObject", + "definitions": + { + "TestObject": + { + "required": + [ + "owner", + "name" + ], + "properties": + { + "owner": + { + "type": "string" + }, + "name": + { + "type": "string" + }, + "config": + { + "patternProperties": + { + ".*": + { + "additionalProperties": true + } + }, + "type": "object" + } + }, + "additionalProperties": false, + "type": "object" + } + } +} diff --git a/tests/data/regressions/issue.51.go.output b/tests/data/regressions/issue.51.go.output new file mode 100644 index 00000000..dd51544d --- /dev/null +++ b/tests/data/regressions/issue.51.go.output @@ -0,0 +1,8 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package test + +type Issue51 struct { + // Name corresponds to the JSON schema field "name". + Name *string `json:"name,omitempty" yaml:"name,omitempty" mapstructure:"name,omitempty"` +} diff --git a/tests/data/regressions/issue.51.json b/tests/data/regressions/issue.51.json new file mode 100644 index 00000000..0994c9a7 --- /dev/null +++ b/tests/data/regressions/issue.51.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "additionalProperties": true, + "properties": { + "name": { + "type": "string" + } + } +} diff --git a/tests/integration_test.go b/tests/integration_test.go index 529a30d8..6363d25f 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -160,6 +160,12 @@ func testExamples(t *testing.T, cfg generator.Config, dataDir string) { } } +func TestRegressions(t *testing.T) { + t.Parallel() + + testExamples(t, basicConfig, "./data/regressions") +} + func testExampleFile(t *testing.T, cfg generator.Config, fileName string) { t.Helper()