Skip to content

Commit

Permalink
Merge pull request #37 from invopop/unique-require
Browse files Browse the repository at this point in the history
Avoid duplicate require entries when composing
  • Loading branch information
samlown committed Jul 25, 2022
2 parents 6fa1420 + 4afb4d8 commit e8e691e
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 43 deletions.
8 changes: 4 additions & 4 deletions fixtures/allow_additional_props.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"TestUser": {
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -38,9 +41,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -202,12 +202,12 @@
},
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
8 changes: 4 additions & 4 deletions fixtures/defaults_expanded_toplevel.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
}
},
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -38,9 +41,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -203,12 +203,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
8 changes: 4 additions & 4 deletions fixtures/ignore_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
"TestUser": {
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -32,9 +35,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -197,12 +197,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
8 changes: 4 additions & 4 deletions fixtures/no_reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/test-user",
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -26,9 +29,6 @@
"MapType": {
"type": "object"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -192,12 +192,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
8 changes: 4 additions & 4 deletions fixtures/no_reference_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"$id": "https://github.com/invopop/jsonschema/test-user",
"$anchor": "TestUser",
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -28,9 +31,6 @@
"MapType": {
"type": "object"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -194,12 +194,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
6 changes: 3 additions & 3 deletions fixtures/required_from_jsontags.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
},
"TestUser": {
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -39,9 +42,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down
8 changes: 4 additions & 4 deletions fixtures/test_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
},
"TestUser": {
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -39,9 +42,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -204,12 +204,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
8 changes: 4 additions & 4 deletions fixtures/test_user_assign_anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"TestUser": {
"$anchor": "TestUser",
"properties": {
"id": {
"type": "integer"
},
"some_base_property": {
"type": "integer"
},
Expand All @@ -41,9 +44,6 @@
"MapType": {
"$ref": "#/$defs/MapType"
},
"id": {
"type": "integer"
},
"name": {
"type": "string",
"maxLength": 20,
Expand Down Expand Up @@ -206,12 +206,12 @@
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"PublicNonExported",
"MapType",
"id",
"name",
"password",
"TestFlag",
Expand Down
11 changes: 10 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (r *Reflector) reflectStructFields(st *Schema, definitions Definitions, t r

st.Properties.Set(name, property)
if required {
st.Required = append(st.Required, name)
st.Required = appendUniqueString(st.Required, name)
}
}

Expand All @@ -572,6 +572,15 @@ func (r *Reflector) reflectStructFields(st *Schema, definitions Definitions, t r
}
}

func appendUniqueString(base []string, value string) []string {
for _, v := range base {
if v == value {
return base
}
}
return append(base, value)
}

func (r *Reflector) lookupComment(t reflect.Type, name string) string {
if r.CommentMap == nil {
return ""
Expand Down
25 changes: 14 additions & 11 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type GrandfatherType struct {
}

type SomeBaseType struct {
SomeBaseProperty int `json:"some_base_property"`
ID string `json:"id"` // to test composition override
SomeBaseProperty int `json:"some_base_property"`
// The jsonschema required tag is nonsensical for private and ignored properties.
// Their presence here tests that the fields *will not* be required in the output
// schema, even if they are tagged required.
Expand Down Expand Up @@ -483,14 +484,16 @@ func TestArrayFormat(t *testing.T) {
TestURIs []string `jsonschema:"type=array,format=uri"`
}

var jsonReflector Reflector
schema := jsonReflector.Reflect(&URIArray{})

URIInterface, found := schema.Definitions["URIArray"].Properties.Get("TestURIs")
require.Equal(t, found, true)

var URIArrayProperties *Schema = URIInterface.(*Schema)

URIArrayType := URIArrayProperties.Items.Format
require.Equal(t, URIArrayType, "uri")
r := new(Reflector)
schema := r.Reflect(&URIArray{})
d := schema.Definitions["URIArray"]
require.NotNil(t, d)
props := d.Properties
require.NotNil(t, props)
i, found := props.Get("TestURIs")
require.True(t, found)

p := i.(*Schema)
pt := p.Items.Format
require.Equal(t, pt, "uri")
}

0 comments on commit e8e691e

Please sign in to comment.