Skip to content

Commit

Permalink
Merge pull request #47 from paulcacheux/paulcacheux/inline-ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown authored Sep 23, 2022
2 parents 4a807f9 + 48f39d6 commit 679d2eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions fixtures/inlining_ptr.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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.Ptr && 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
Original file line number Diff line number Diff line change
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

0 comments on commit 679d2eb

Please sign in to comment.