Skip to content

Commit

Permalink
chore: add regression tests for additionalProperties as highlighted i…
Browse files Browse the repository at this point in the history
…n issue #32 and #51
  • Loading branch information
omissis committed May 1, 2023
1 parent 93f5473 commit e830c7f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/data/regressions/issue.32.go.output
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions tests/data/regressions/issue.32.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
8 changes: 8 additions & 0 deletions tests/data/regressions/issue.51.go.output
Original file line number Diff line number Diff line change
@@ -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"`
}
10 changes: 10 additions & 0 deletions tests/data/regressions/issue.51.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true,
"properties": {
"name": {
"type": "string"
}
}
}
6 changes: 6 additions & 0 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit e830c7f

Please sign in to comment.