From 36159b88d98656aabadec99b5c6ff54f250f1b36 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Tue, 23 Nov 2021 14:04:41 -0500 Subject: [PATCH 1/4] Do not clear refs on recursive type references --- openapi3gen/openapi3gen.go | 4 +++- openapi3gen/openapi3gen_test.go | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go index b4ae7b04c..b21a4e862 100644 --- a/openapi3gen/openapi3gen.go +++ b/openapi3gen/openapi3gen.go @@ -57,7 +57,9 @@ func NewSchemaRefForValue(value interface{}, opts ...Option) (*openapi3.SchemaRe g := NewGenerator(opts...) ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) for ref := range g.SchemaRefs { - ref.Ref = "" + if !strings.HasPrefix(ref.Ref, "#/") { + ref.Ref = "" + } } return ref, g.SchemaRefs, err } diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go index 6d96db98e..79f74bf46 100644 --- a/openapi3gen/openapi3gen_test.go +++ b/openapi3gen/openapi3gen_test.go @@ -246,3 +246,41 @@ func TestSchemaCustomizerError(t *testing.T) { })) require.EqualError(t, err, "test error") } + +func TestRecursiveSchema(t *testing.T) { + + type RecursiveType struct { + Field1 string `json:"field1"` + Field2 string `json:"field2"` + Field3 string `json:"field3"` + Components []*RecursiveType `json:"children,omitempty"` + } + + schemaRef, _, err := NewSchemaRefForValue(&RecursiveType{}) + require.NoError(t, err) + + jsonSchema, err := schemaRef.MarshalJSON() + require.NoError(t, err) + + require.JSONEq(t, `{ + "properties": { + "children": { + "items": { + "$ref": "#/components/schemas/RecursiveType" + }, + "type": "array" + }, + "field1": { + "type": "string" + }, + "field2": { + "type": "string" + }, + "field3": { + "type": "string" + } + }, + "type": "object" + }`, string(jsonSchema)) + +} From 2f25aeab5455a530d70dc514da430abd956f2760 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Tue, 23 Nov 2021 23:41:22 -0500 Subject: [PATCH 2/4] Provide a new function to return dependent schemas --- openapi3gen/openapi3gen.go | 32 +++++++++++++++++++++++++++----- openapi3gen/openapi3gen_test.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go index b21a4e862..8ed8f1881 100644 --- a/openapi3gen/openapi3gen.go +++ b/openapi3gen/openapi3gen.go @@ -57,11 +57,28 @@ func NewSchemaRefForValue(value interface{}, opts ...Option) (*openapi3.SchemaRe g := NewGenerator(opts...) ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) for ref := range g.SchemaRefs { - if !strings.HasPrefix(ref.Ref, "#/") { + ref.Ref = "" + } + return ref, g.SchemaRefs, err +} + +// NewSchemaRefAndComponentsForValue returns the ref for this schema, and an array of dependent component schemas +func NewSchemaRefAndComponentsForValue(value interface{}, schemas openapi3.Schemas, opts ...Option) (*openapi3.SchemaRef, error) { + g := NewGenerator(opts...) + ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) + for ref := range g.SchemaRefs { + if g.ComponentSchemas[ref.Ref] { + schemas[ref.Ref] = &openapi3.SchemaRef{ + Value: ref.Value, + } + } + if strings.HasPrefix(ref.Ref, "#/components/schemas/") { + ref.Value = nil + } else { ref.Ref = "" } } - return ref, g.SchemaRefs, err + return ref, err } type Generator struct { @@ -73,6 +90,9 @@ type Generator struct { // If count is 1, it's not ne // An OpenAPI identifier has been assigned to each. SchemaRefs map[*openapi3.SchemaRef]int + + // ComponentSchemas contains a map of schemas that must be defined in the components, due to cycles + ComponentSchemas map[string]bool } func NewGenerator(opts ...Option) *Generator { @@ -81,9 +101,10 @@ func NewGenerator(opts ...Option) *Generator { f(gOpt) } return &Generator{ - Types: make(map[reflect.Type]*openapi3.SchemaRef), - SchemaRefs: make(map[*openapi3.SchemaRef]int), - opts: *gOpt, + Types: make(map[reflect.Type]*openapi3.SchemaRef), + SchemaRefs: make(map[*openapi3.SchemaRef]int), + ComponentSchemas: make(map[string]bool), + opts: *gOpt, } } @@ -343,6 +364,7 @@ func (g *Generator) generateCycleSchemaRef(t reflect.Type, schema *openapi3.Sche typeName = t.Name() } + g.ComponentSchemas[typeName] = true return openapi3.NewSchemaRef(fmt.Sprintf("#/components/schemas/%s", typeName), schema) } diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go index 79f74bf46..64be849e8 100644 --- a/openapi3gen/openapi3gen_test.go +++ b/openapi3gen/openapi3gen_test.go @@ -256,12 +256,39 @@ func TestRecursiveSchema(t *testing.T) { Components []*RecursiveType `json:"children,omitempty"` } - schemaRef, _, err := NewSchemaRefForValue(&RecursiveType{}) + schemas := make(openapi3.Schemas) + schemaRef, err := NewSchemaRefAndComponentsForValue(&RecursiveType{}, schemas) require.NoError(t, err) - jsonSchema, err := schemaRef.MarshalJSON() + jsonSchemas, err := json.MarshalIndent(&schemas, "", " ") require.NoError(t, err) + jsonSchemaRef, err := json.MarshalIndent(&schemaRef, "", " ") + require.NoError(t, err) + + require.JSONEq(t, `{ + "RecursiveType": { + "properties": { + "children": { + "items": { + "$ref": "#/components/schemas/RecursiveType" + }, + "type": "array" + }, + "field1": { + "type": "string" + }, + "field2": { + "type": "string" + }, + "field3": { + "type": "string" + } + }, + "type": "object" + } + }`, string(jsonSchemas)) + require.JSONEq(t, `{ "properties": { "children": { @@ -281,6 +308,6 @@ func TestRecursiveSchema(t *testing.T) { } }, "type": "object" - }`, string(jsonSchema)) + }`, string(jsonSchemaRef)) } From d0b8b6307f84a57fde3c0f6df84f628f4f547e25 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Wed, 24 Nov 2021 14:47:40 -0500 Subject: [PATCH 3/4] Update syntax of NewSchemaRefForValue, rather than new method --- openapi3gen/openapi3gen.go | 46 ++++++------- openapi3gen/openapi3gen_test.go | 110 ++++++++++++++++++++++++++++++-- openapi3gen/simple_test.go | 6 +- 3 files changed, 124 insertions(+), 38 deletions(-) diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go index 8ed8f1881..7b874be5d 100644 --- a/openapi3gen/openapi3gen.go +++ b/openapi3gen/openapi3gen.go @@ -52,33 +52,10 @@ func SchemaCustomizer(sc SchemaCustomizerFn) Option { return func(x *generatorOpt) { x.schemaCustomizer = sc } } -// NewSchemaRefForValue uses reflection on the given value to produce a SchemaRef. -func NewSchemaRefForValue(value interface{}, opts ...Option) (*openapi3.SchemaRef, map[*openapi3.SchemaRef]int, error) { +// NewSchemaRefForValue returns the ref for this schema, and an array of dependent component schemas +func NewSchemaRefForValue(value interface{}, schemas openapi3.Schemas, opts ...Option) (*openapi3.SchemaRef, error) { g := NewGenerator(opts...) - ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) - for ref := range g.SchemaRefs { - ref.Ref = "" - } - return ref, g.SchemaRefs, err -} - -// NewSchemaRefAndComponentsForValue returns the ref for this schema, and an array of dependent component schemas -func NewSchemaRefAndComponentsForValue(value interface{}, schemas openapi3.Schemas, opts ...Option) (*openapi3.SchemaRef, error) { - g := NewGenerator(opts...) - ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) - for ref := range g.SchemaRefs { - if g.ComponentSchemas[ref.Ref] { - schemas[ref.Ref] = &openapi3.SchemaRef{ - Value: ref.Value, - } - } - if strings.HasPrefix(ref.Ref, "#/components/schemas/") { - ref.Value = nil - } else { - ref.Ref = "" - } - } - return ref, err + return g.newSchemaRefForValue(value, schemas) } type Generator struct { @@ -113,6 +90,23 @@ func (g *Generator) GenerateSchemaRef(t reflect.Type) (*openapi3.SchemaRef, erro return g.generateSchemaRefFor(nil, t, "_root", "") } +func (g *Generator) newSchemaRefForValue(value interface{}, schemas openapi3.Schemas) (*openapi3.SchemaRef, error) { + ref, err := g.GenerateSchemaRef(reflect.TypeOf(value)) + for ref := range g.SchemaRefs { + if g.ComponentSchemas[ref.Ref] && schemas != nil { + schemas[ref.Ref] = &openapi3.SchemaRef{ + Value: ref.Value, + } + } + if strings.HasPrefix(ref.Ref, "#/components/schemas/") { + ref.Value = nil + } else { + ref.Ref = "" + } + } + return ref, err +} + func (g *Generator) generateSchemaRefFor(parents []*jsoninfo.TypeInfo, t reflect.Type, name string, tag reflect.StructTag) (*openapi3.SchemaRef, error) { if ref := g.Types[t]; ref != nil && g.opts.schemaCustomizer == nil { g.SchemaRefs[ref]++ diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go index 64be849e8..ee8530c8f 100644 --- a/openapi3gen/openapi3gen_test.go +++ b/openapi3gen/openapi3gen_test.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/getkin/kin-openapi/openapi3" "github.com/stretchr/testify/require" @@ -19,11 +20,106 @@ type CyclicType1 struct { CyclicField *CyclicType0 `json:"b"` } +func TestSimpleStruct(t *testing.T) { + type SomeOtherType string + + type SomeStruct struct { + Bool bool `json:"bool"` + Int int `json:"int"` + Int64 int64 `json:"int64"` + Float64 float64 `json:"float64"` + String string `json:"string"` + Bytes []byte `json:"bytes"` + JSON json.RawMessage `json:"json"` + Time time.Time `json:"time"` + Slice []SomeOtherType `json:"slice"` + Map map[string]*SomeOtherType `json:"map"` + + Struct struct { + X string `json:"x"` + } `json:"struct"` + + EmptyStruct struct { + Y string + } `json:"structWithoutFields"` + + Ptr *SomeOtherType `json:"ptr"` + } + + g := NewGenerator() + schemaRef, err := g.newSchemaRefForValue(&SomeStruct{}, nil) + require.NoError(t, err) + require.Len(t, g.SchemaRefs, 15) + + schemaJSON, err := json.Marshal(schemaRef) + require.NoError(t, err) + + require.JSONEq(t, ` + { + "properties": { + "bool": { + "type": "boolean" + }, + "bytes": { + "format": "byte", + "type": "string" + }, + "float64": { + "format": "double", + "type": "number" + }, + "int": { + "type": "integer" + }, + "int64": { + "format": "int64", + "type": "integer" + }, + "json": {}, + "map": { + "additionalProperties": { + "type": "string" + }, + "type": "object" + }, + "ptr": { + "type": "string" + }, + "slice": { + "items": { + "type": "string" + }, + "type": "array" + }, + "string": { + "type": "string" + }, + "struct": { + "properties": { + "x": { + "type": "string" + } + }, + "type": "object" + }, + "structWithoutFields": {}, + "time": { + "format": "date-time", + "type": "string" + } + }, + "type": "object" + } + `, string(schemaJSON)) + +} + func TestCyclic(t *testing.T) { - schemaRef, refsMap, err := NewSchemaRefForValue(&CyclicType0{}, ThrowErrorOnCycle()) + g := NewGenerator(ThrowErrorOnCycle()) + schemaRef, err := g.newSchemaRefForValue(&CyclicType0{}, nil) require.IsType(t, &CycleError{}, err) require.Nil(t, schemaRef) - require.Empty(t, refsMap) + require.Empty(t, g.SchemaRefs) } func TestExportedNonTagged(t *testing.T) { @@ -34,7 +130,7 @@ func TestExportedNonTagged(t *testing.T) { EvenAYaml string `yaml:"even_a_yaml"` } - schemaRef, _, err := NewSchemaRefForValue(&Bla{}, UseAllExportedFields()) + schemaRef, err := NewSchemaRefForValue(&Bla{}, nil, UseAllExportedFields()) require.NoError(t, err) require.Equal(t, &openapi3.SchemaRef{Value: &openapi3.Schema{ Type: "object", @@ -50,7 +146,7 @@ func TestExportUint(t *testing.T) { UnsignedInt uint `json:"uint"` } - schemaRef, _, err := NewSchemaRefForValue(&UnsignedIntStruct{}, UseAllExportedFields()) + schemaRef, err := NewSchemaRefForValue(&UnsignedIntStruct{}, nil, UseAllExportedFields()) require.NoError(t, err) require.Equal(t, &openapi3.SchemaRef{Value: &openapi3.Schema{ Type: "object", @@ -169,7 +265,7 @@ func TestSchemaCustomizer(t *testing.T) { EnumField3 string `json:"enum3" myenumtag:"e,f"` } - schemaRef, _, err := NewSchemaRefForValue(&Bla{}, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error { + schemaRef, err := NewSchemaRefForValue(&Bla{}, nil, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error { t.Logf("Field=%s,Tag=%s", name, tag) if tag.Get("mymintag") != "" { minVal, err := strconv.ParseFloat(tag.Get("mymintag"), 64) @@ -241,7 +337,7 @@ func TestSchemaCustomizer(t *testing.T) { func TestSchemaCustomizerError(t *testing.T) { type Bla struct{} - _, _, err := NewSchemaRefForValue(&Bla{}, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error { + _, err := NewSchemaRefForValue(&Bla{}, nil, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error { return errors.New("test error") })) require.EqualError(t, err, "test error") @@ -257,7 +353,7 @@ func TestRecursiveSchema(t *testing.T) { } schemas := make(openapi3.Schemas) - schemaRef, err := NewSchemaRefAndComponentsForValue(&RecursiveType{}, schemas) + schemaRef, err := NewSchemaRefForValue(&RecursiveType{}, schemas) require.NoError(t, err) jsonSchemas, err := json.MarshalIndent(&schemas, "", " ") diff --git a/openapi3gen/simple_test.go b/openapi3gen/simple_test.go index d997e23b2..99e94ae12 100644 --- a/openapi3gen/simple_test.go +++ b/openapi3gen/simple_test.go @@ -36,15 +36,11 @@ type ( ) func Example() { - schemaRef, refsMap, err := openapi3gen.NewSchemaRefForValue(&SomeStruct{}) + schemaRef, err := openapi3gen.NewSchemaRefForValue(&SomeStruct{}, nil) if err != nil { panic(err) } - if len(refsMap) != 15 { - panic(fmt.Sprintf("unintended len(refsMap) = %d", len(refsMap))) - } - data, err := json.MarshalIndent(schemaRef, "", " ") if err != nil { panic(err) From f24e6dc2ad4529f7d73ba74bff12662c69ee23c5 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Wed, 24 Nov 2021 14:51:55 -0500 Subject: [PATCH 4/4] Re-instate original comment --- openapi3gen/openapi3gen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go index 7b874be5d..1c233be12 100644 --- a/openapi3gen/openapi3gen.go +++ b/openapi3gen/openapi3gen.go @@ -52,7 +52,7 @@ func SchemaCustomizer(sc SchemaCustomizerFn) Option { return func(x *generatorOpt) { x.schemaCustomizer = sc } } -// NewSchemaRefForValue returns the ref for this schema, and an array of dependent component schemas +// NewSchemaRefForValue uses reflection on the given value to produce a SchemaRef, and updates a supplied map with any dependent component schemas (for cycles) func NewSchemaRefForValue(value interface{}, schemas openapi3.Schemas, opts ...Option) (*openapi3.SchemaRef, error) { g := NewGenerator(opts...) return g.newSchemaRefForValue(value, schemas)