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 anonymous ptr to struct inlining #47

Merged
merged 2 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions 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"
]
}
5 changes: 5 additions & 0 deletions reflect.go
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions reflect_test.go
Expand Up @@ -162,6 +162,11 @@ type OuterNamed struct {
Inner `json:"inner"`
}

type OuterPtr struct {
*Inner
Text `json:",omitempty"`
}

type Inner struct {
Foo string `yaml:"foo"`
}
Expand Down Expand Up @@ -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{
Expand Down