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

Optionally expose embedded structs with allOf #107

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ linters-settings:
linters:
enable-all: true
disable:
- maintidx
- musttag
- containedctx
- goerr113
Expand Down
24 changes: 23 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
typeOfTextMarshaler = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
typeOfEmptyInterface = reflect.TypeOf((*interface{})(nil)).Elem()
typeOfSchemaInliner = reflect.TypeOf((*SchemaInliner)(nil)).Elem()
typeOfEmbedReferencer = reflect.TypeOf((*EmbedReferencer)(nil)).Elem()
)

const (
Expand All @@ -47,6 +48,11 @@
InlineJSONSchema()
}

// EmbedReferencer is a marker interface to enable reference to embedded struct type.
type EmbedReferencer interface {
ReferEmbedded()
}

// IgnoreTypeName instructs reflector to keep original type name during mapping.
func (s Schema) IgnoreTypeName() {}

Expand Down Expand Up @@ -261,6 +267,10 @@
// ProcessWithoutTags
// SkipEmbeddedMapsSlices
// SkipUnsupportedProperties
//
// Fields from embedded structures are processed as if they were defined in the root structure.
// Alternatively, if embedded structure has a field tag `refer:"true"` or implements EmbedReferencer,
// its reference will be added to `allOf` of the parent schema.
func (r *Reflector) Reflect(i interface{}, options ...func(rc *ReflectContext)) (Schema, error) {
rc := ReflectContext{}
rc.Context = context.Background()
Expand Down Expand Up @@ -914,7 +924,19 @@

if tag == "" && field.Anonymous &&
(field.Type.Kind() == reflect.Struct || deepIndirect.Kind() == reflect.Struct) {
if err := r.walkProperties(values[i], parent, rc); err != nil {
forceReference := (field.Type.Implements(typeOfEmbedReferencer) && field.Tag.Get("refer") == "") ||
field.Tag.Get("refer") == "true"

if forceReference {
rc.Path = append(rc.Path, "")

s, err := r.reflect(values[i].Interface(), rc, false, parent)
if err != nil {
return err

Check warning on line 935 in reflect.go

View check run for this annotation

Codecov / codecov/patch

reflect.go#L935

Added line #L935 was not covered by tests
}

Check notice on line 936 in reflect.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 934:936 are not covered by tests.

parent.AllOf = append(parent.AllOf, s.ToSchemaOrBool())
} else if err := r.walkProperties(values[i], parent, rc); err != nil {
return err
}

Expand Down
33 changes: 33 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,36 @@ func TestReflector_Reflect_multipleTags(t *testing.T) {
"type":"object"
}`, s)
}

func TestReflector_Reflect_embedded(t *testing.T) {
type A struct {
FieldA int `json:"field_a"`
}

type C struct {
jsonschema.EmbedReferencer
FieldC int `json:"field_c"`
}

type B struct {
A `refer:"true"`
FieldB int `json:"field_b"`
C
}

r := jsonschema.Reflector{}

s, err := r.Reflect(B{})
require.NoError(t, err)
assertjson.EqMarshal(t, `{
"definitions":{
"JsonschemaGoTestA":{"properties":{"field_a":{"type":"integer"}},"type":"object"},
"JsonschemaGoTestC":{"properties":{"field_c":{"type":"integer"}},"type":"object"}
},
"properties":{"field_b":{"type":"integer"}},"type":"object",
"allOf":[
{"$ref":"#/definitions/JsonschemaGoTestA"},
{"$ref":"#/definitions/JsonschemaGoTestC"}
]
}`, s)
}
Loading