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

Support reference cycles #393

Merged
merged 8 commits into from Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
54 changes: 50 additions & 4 deletions openapi3gen/openapi3gen.go
Expand Up @@ -3,6 +3,7 @@ package openapi3gen

import (
"encoding/json"
"fmt"
"math"
"reflect"
"strings"
Expand Down Expand Up @@ -104,6 +105,11 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
if a && b {
vs, err := g.generateSchemaRefFor(parents, v.Type)
if err != nil {
// TODO: this needs code review
DerekStrickland marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := err.(*CycleError); ok {
g.SchemaRefs[vs]++
return vs, nil
}
return nil, err
}
refSchemaRef := RefSchemaRef
Expand Down Expand Up @@ -185,7 +191,11 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
schema.Type = "array"
items, err := g.generateSchemaRefFor(parents, t.Elem())
if err != nil {
return nil, err
if _, ok := err.(*CycleError); ok {
items = g.generateCycleSchemaRef(t.Elem(), schema)
} else {
return nil, err
}
}
if items != nil {
g.SchemaRefs[items]++
Expand All @@ -197,7 +207,11 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
schema.Type = "object"
additionalProperties, err := g.generateSchemaRefFor(parents, t.Elem())
if err != nil {
return nil, err
if _, ok := err.(*CycleError); ok {
additionalProperties = g.generateCycleSchemaRef(t.Elem(), schema)
} else {
return nil, err
}
}
if additionalProperties != nil {
g.SchemaRefs[additionalProperties]++
Expand All @@ -221,7 +235,11 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
if t.Field(fieldInfo.Index[0]).Anonymous {
ref, err := g.generateSchemaRefFor(parents, fType)
if err != nil {
return nil, err
if _, ok := err.(*CycleError); ok {
ref = g.generateCycleSchemaRef(fType, schema)
} else {
return nil, err
}
}
if ref != nil {
g.SchemaRefs[ref]++
Expand All @@ -237,7 +255,11 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec

ref, err := g.generateSchemaRefFor(parents, fType)
if err != nil {
return nil, err
if _, ok := err.(*CycleError); ok {
ref = g.generateCycleSchemaRef(fType, schema)
} else {
return nil, err
}
}
if ref != nil {
g.SchemaRefs[ref]++
Expand All @@ -255,6 +277,30 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
return openapi3.NewSchemaRef(t.Name(), schema), nil
}

func (g *Generator) generateCycleSchemaRef(t reflect.Type, schema *openapi3.Schema) *openapi3.SchemaRef {
var typeName string
switch t.Kind() {
case reflect.Ptr:
return g.generateCycleSchemaRef(t.Elem(), schema)
case reflect.Slice:
ref := g.generateCycleSchemaRef(t.Elem(), schema)
sliceSchema := openapi3.NewSchema()
sliceSchema.Type = "array"
sliceSchema.Items = ref
return openapi3.NewSchemaRef("", sliceSchema)
case reflect.Map:
ref := g.generateCycleSchemaRef(t.Elem(), schema)
mapSchema := openapi3.NewSchema()
mapSchema.Type = "object"
mapSchema.AdditionalProperties = ref
return openapi3.NewSchemaRef("", mapSchema)
default:
typeName = t.Name()
}

return openapi3.NewSchemaRef(fmt.Sprintf("#/components/schemas/%s", typeName), schema)
}

var RefSchemaRef = openapi3.NewSchemaRef("Ref",
openapi3.NewObjectSchema().WithProperty("$ref", openapi3.NewStringSchema().WithMinLength(1)))

Expand Down
45 changes: 31 additions & 14 deletions openapi3gen/openapi3gen_test.go
Expand Up @@ -8,20 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

type CyclicType0 struct {
CyclicField *CyclicType1 `json:"a"`
}
type CyclicType1 struct {
CyclicField *CyclicType0 `json:"b"`
}

func TestCyclic(t *testing.T) {
fenollp marked this conversation as resolved.
Show resolved Hide resolved
schemaRef, refsMap, err := NewSchemaRefForValue(&CyclicType0{})
require.IsType(t, &CycleError{}, err)
require.Nil(t, schemaRef)
require.Empty(t, refsMap)
}

func TestExportedNonTagged(t *testing.T) {
type Bla struct {
A string
Expand Down Expand Up @@ -84,3 +70,34 @@ func TestEmbeddedStructs(t *testing.T) {
_, ok = schemaRef.Value.Properties["ID"]
require.Equal(t, true, ok)
}

func TestCyclicReferences(t *testing.T) {
type ObjectDiff struct {
FieldCycle *ObjectDiff
SliceCycle []*ObjectDiff
MapCycle map[*ObjectDiff]*ObjectDiff
}

instance := &ObjectDiff{
FieldCycle: nil,
SliceCycle: nil,
MapCycle: nil,
}

generator := NewGenerator(UseAllExportedFields())

schemaRef, err := generator.GenerateSchemaRef(reflect.TypeOf(instance))
require.NoError(t, err)

require.NotNil(t, schemaRef.Value.Properties["FieldCycle"])
require.Equal(t, "#/components/schemas/ObjectDiff", schemaRef.Value.Properties["FieldCycle"].Ref)

require.NotNil(t, schemaRef.Value.Properties["SliceCycle"])
require.Equal(t, "array", schemaRef.Value.Properties["SliceCycle"].Value.Type)
require.Equal(t, "#/components/schemas/ObjectDiff", schemaRef.Value.Properties["SliceCycle"].Value.Items.Ref)

require.NotNil(t, schemaRef.Value.Properties["MapCycle"])
require.Equal(t, "object", schemaRef.Value.Properties["MapCycle"].Value.Type)
require.Equal(t, "#/components/schemas/ObjectDiff", schemaRef.Value.Properties["MapCycle"].Value.AdditionalProperties.Ref)

}