Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Proper handling of clashing types case #109

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 123 additions & 0 deletions fixtures/clashing_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/odd",
"$ref": "#/$defs/Odd",
"$defs": {
"Dummy": {
"properties": {
"A": {
"type": "string"
},
"Dummy": {
"$ref": "#/$defs/Dummy-1"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"A",
"Dummy"
]
},
"Dummy-1": {
"properties": {
"A": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"A"
]
},
"Dummy-2": {
"properties": {
"B": {
"type": "integer"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"B"
]
},
"GrandfatherType": {
"properties": {
"family_name": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"family_name"
]
},
"Odd": {
"properties": {
"id": {
"type": "string"
},
"some_base_property": {
"type": "integer"
},
"grand": {
"$ref": "#/$defs/GrandfatherType"
},
"SomeUntaggedBaseProperty": {
"type": "boolean"
},
"internal": {
"$ref": "#/$defs/Odd-1"
},
"link": {
"$ref": "#/$defs/Odd"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"id",
"some_base_property",
"grand",
"SomeUntaggedBaseProperty",
"internal",
"link"
]
},
"Odd-1": {
"properties": {
"Dummy1": {
"$ref": "#/$defs/Dummy"
},
"Dummy1a": {
"$ref": "#/$defs/Dummy"
},
"Dummy2": {
"$ref": "#/$defs/Dummy-1"
},
"Dummy2a": {
"$ref": "#/$defs/Dummy-1"
},
"Dummy3": {
"$ref": "#/$defs/Dummy-2"
},
"Dummy3a": {
"$ref": "#/$defs/Dummy-2"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"Dummy1",
"Dummy1a",
"Dummy2",
"Dummy2a",
"Dummy3",
"Dummy3a"
]
}
}
}
51 changes: 51 additions & 0 deletions fixtures/shadowed_clashing_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/grandfather-type",
"$ref": "#/$defs/GrandfatherType",
"$defs": {
"GrandfatherType": {
"properties": {
"odd": {
"$ref": "#/$defs/Odd"
},
"link": {
"$ref": "#/$defs/GrandfatherType"
},
"pkg_link": {
"$ref": "#/$defs/GrandfatherType-1"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"odd",
"link",
"pkg_link"
]
},
"GrandfatherType-1": {
"properties": {
"family_name": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"family_name"
]
},
"Odd": {
"properties": {
"base": {
"$ref": "#/$defs/GrandfatherType-1"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"base"
]
}
}
}
22 changes: 22 additions & 0 deletions internal/testdata/odd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package testdata

import (
"github.com/invopop/jsonschema/internal/testdata/types"
"github.com/invopop/jsonschema/internal/testdata/types/deeper"
)

type (
Odd struct {
Dummy1 types.Dummy
Dummy1a types.Dummy

Dummy2 deeper.Dummy
Dummy2a deeper.Dummy

Dummy3 Dummy
Dummy3a Dummy
}
Dummy struct {
B int
}
)
5 changes: 5 additions & 0 deletions internal/testdata/types/deeper/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package deeper

type Dummy struct {
A string
}
8 changes: 8 additions & 0 deletions internal/testdata/types/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

import "github.com/invopop/jsonschema/internal/testdata/types/deeper"

type Dummy struct {
A string
Dummy deeper.Dummy
}
45 changes: 39 additions & 6 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,10 @@ func (r *Reflector) lookupComment(t reflect.Type, name string) string {

// addDefinition will append the provided schema. If needed, an ID and anchor will also be added.
func (r *Reflector) addDefinition(definitions Definitions, t reflect.Type, s *Schema) {
name := r.typeName(t)
// we save both type & pkg info to match against reflected type later
s._type = t

_, name := r.findDef(definitions, t)
if name == "" {
return
}
Expand All @@ -585,18 +588,48 @@ func (r *Reflector) refDefinition(definitions Definitions, t reflect.Type) *Sche
if r.DoNotReference {
return nil
}
name := r.typeName(t)
if name == "" {
return nil
}
if _, ok := definitions[name]; !ok {

def, name := r.findDef(definitions, t)
if def == nil {
// no entry present in definitions
// This is also true if name == "" (~ r.typeName() == "")
return nil
}
return &Schema{
Ref: "#/$defs/" + name,
}
}

// findDef returns the matching definition for the passed reflect.Type
// It will add suffix like `_idx` if necessary & return the corresponding key as well.
// If no applicable entry is available, it will return nil & the key available to use.
func (r *Reflector) findDef(definitions Definitions, t reflect.Type) (*Schema, string) {
name := r.typeName(t)
if name == "" {
return nil, ""
}

defName := name
for idx := 1; ; idx++ {
def, ok := definitions[defName]
if !ok {
return nil, defName
}
if sameReflectTypes(def._type, t) {
return def, defName
}
defName = name + "-" + strconv.Itoa(idx)
}
}

func sameReflectTypes(a, b reflect.Type) bool {
if fullyQualifiedTypeName(a) != fullyQualifiedTypeName(b) {
return false
}

return a.AssignableTo(b) && b.AssignableTo(a)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is required for shadowing case, see TestShadowedClashingTypes

}

func (r *Reflector) lookupID(t reflect.Type) ID {
if r.Lookup != nil {
if t.Kind() == reflect.Ptr {
Expand Down
32 changes: 31 additions & 1 deletion reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"

"github.com/invopop/jsonschema/examples"

"github.com/invopop/jsonschema/internal/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -657,3 +657,33 @@ func TestJSONSchemaAlias(t *testing.T) {
compareSchemaOutput(t, "fixtures/schema_alias.json", r, &AliasObjectB{})
compareSchemaOutput(t, "fixtures/schema_alias_2.json", r, &AliasObjectC{})
}

func TestClashingTypes(t *testing.T) {
type Odd struct {
SomeBaseType
Internal testdata.Odd `json:"internal"`
Link *Odd `json:"link"`
}

r := &Reflector{}
compareSchemaOutput(t, "fixtures/clashing_types.json", r, &Odd{})
}

func TestShadowedClashingTypes(t *testing.T) {
type (
Odd struct {
Base GrandfatherType `json:"base"`
}
GrandfatherTypePtr *GrandfatherType
)

{
type GrandfatherType struct {
Odd Odd `json:"odd"`
Link *GrandfatherType `json:"link"`
PkgLink GrandfatherTypePtr `json:"pkg_link"`
}
r := &Reflector{}
compareSchemaOutput(t, "fixtures/shadowed_clashing_types.json", r, &GrandfatherType{})
}
}
4 changes: 4 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonschema

import (
"encoding/json"
"reflect"

orderedmap "github.com/wk8/go-ordered-map/v2"
)
Expand Down Expand Up @@ -79,6 +80,9 @@ type Schema struct {

// Special boolean representation of the Schema - section 4.3.2
boolean *bool

// _type is used to define whether the looked-up definition points to the proper type or not
_type reflect.Type
}

var (
Expand Down