From 0ee4215a6859e63504624d560519b3fe90eb448f Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Fri, 16 Sep 2022 14:47:46 +0200 Subject: [PATCH 1/2] Fix anonymous ptr to struct inlining --- fixtures/inlining_ptr.json | 17 +++++++++++++++++ reflect.go | 5 +++++ reflect_test.go | 6 ++++++ 3 files changed, 28 insertions(+) create mode 100644 fixtures/inlining_ptr.json diff --git a/fixtures/inlining_ptr.json b/fixtures/inlining_ptr.json new file mode 100644 index 0000000..9ea4738 --- /dev/null +++ b/fixtures/inlining_ptr.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/invopop/jsonschema/outer-ptr", + "properties": { + "Foo": { + "type": "string" + }, + "Text": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "Foo" + ] +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 36ee30a..4d30df6 100644 --- a/reflect.go +++ b/reflect.go @@ -965,6 +965,11 @@ func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool, if f.Type.Kind() == reflect.Struct { return "", true, false, false } + + // As per JSON Marshal rules, anonymous pointer to structs are inherited + if f.Type.Kind() == reflect.Pointer && f.Type.Elem().Kind() == reflect.Struct { + return "", true, false, false + } } // Try to determine the name from the different combos diff --git a/reflect_test.go b/reflect_test.go index 72e6922..1442bd3 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -162,6 +162,11 @@ type OuterNamed struct { Inner `json:"inner"` } +type OuterPtr struct { + *Inner + Text `json:",omitempty"` +} + type Inner struct { Foo string `yaml:"foo"` } @@ -371,6 +376,7 @@ func TestSchemaGeneration(t *testing.T) { {&Outer{}, &Reflector{ExpandedStruct: true}, "fixtures/inlining_inheritance.json"}, {&OuterNamed{}, &Reflector{ExpandedStruct: true}, "fixtures/inlining_embedded.json"}, {&OuterNamed{}, &Reflector{ExpandedStruct: true, AssignAnchor: true}, "fixtures/inlining_embedded_anchored.json"}, + {&OuterPtr{}, &Reflector{ExpandedStruct: true}, "fixtures/inlining_ptr.json"}, {&MinValue{}, &Reflector{}, "fixtures/schema_with_minimum.json"}, {&TestNullable{}, &Reflector{}, "fixtures/nullable.json"}, {&GrandfatherType{}, &Reflector{ From 48f39d637245af1fd1ad8c116602569776bffba5 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Fri, 23 Sep 2022 16:19:29 -0400 Subject: [PATCH 2/2] fix `Pointer` issue because of recent go version --- reflect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflect.go b/reflect.go index 4d30df6..af36886 100644 --- a/reflect.go +++ b/reflect.go @@ -967,7 +967,7 @@ func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool, } // As per JSON Marshal rules, anonymous pointer to structs are inherited - if f.Type.Kind() == reflect.Pointer && f.Type.Elem().Kind() == reflect.Struct { + if f.Type.Kind() == reflect.Ptr && f.Type.Elem().Kind() == reflect.Struct { return "", true, false, false } }