Skip to content

Commit

Permalink
fix full type details not being able to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
pvormste committed Nov 2, 2023
1 parent 70819d7 commit ec10bd5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/openapi/fixtures/v3.0.0/example_oas7.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Mutation {
replaceDeviceByName(deviceInput: DeviceInput!, deviceName: String!): Device
}

"A device is an object connected to the network"
type Device {
"The device name in the network"
name: String!
Expand Down
62 changes: 53 additions & 9 deletions pkg/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ import (
"strconv"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/iancoleman/strcase"

"github.com/TykTechnologies/graphql-go-tools/pkg/ast"
"github.com/TykTechnologies/graphql-go-tools/pkg/introspection"
"github.com/TykTechnologies/graphql-go-tools/pkg/lexer/literal"
"github.com/TykTechnologies/graphql-go-tools/pkg/operationreport"
"github.com/getkin/kin-openapi/openapi3"
"github.com/iancoleman/strcase"
)

type converter struct {
openapi *openapi3.T
knownFullTypes map[string]struct{}
knownFullTypes map[string]*knownFullTypeDetails
fullTypes []introspection.FullType
}

type knownFullTypeDetails struct {
hasDescription bool
}

func isValidResponse(status int) bool {
if status >= 200 && status < 300 {
return true
Expand Down Expand Up @@ -203,7 +208,7 @@ func (c *converter) processArray(schema *openapi3.SchemaRef) error {
if ok {
return nil
}
c.knownFullTypes[fullTypeName] = struct{}{}
c.knownFullTypes[fullTypeName] = &knownFullTypeDetails{}

ft := introspection.FullType{
Kind: introspection.OBJECT,
Expand Down Expand Up @@ -231,11 +236,21 @@ func (c *converter) processArray(schema *openapi3.SchemaRef) error {

func (c *converter) processObject(schema *openapi3.SchemaRef) error {
fullTypeName := extractFullTypeNameFromRef(schema.Ref)
_, ok := c.knownFullTypes[fullTypeName]
details, ok := c.knownFullTypes[fullTypeName]
if ok {
return nil
needsUpdate := checkForNewKnownFullTypeDetails(schema, details)
if !needsUpdate {
return nil
}

ok = c.updateFullTypeDetails(schema, fullTypeName)
if ok {
return nil
}
}
c.knownFullTypes[fullTypeName] = &knownFullTypeDetails{
hasDescription: len(schema.Value.Description) > 0,
}
c.knownFullTypes[fullTypeName] = struct{}{}

ft := introspection.FullType{
Kind: introspection.OBJECT,
Expand All @@ -256,7 +271,7 @@ func (c *converter) processInputObject(schema *openapi3.SchemaRef) error {
if ok {
return nil
}
c.knownFullTypes[fullTypeName] = struct{}{}
c.knownFullTypes[fullTypeName] = &knownFullTypeDetails{}

ft := introspection.FullType{
Kind: introspection.INPUTOBJECT,
Expand Down Expand Up @@ -624,10 +639,31 @@ func (c *converter) importMutationType() (*introspection.FullType, error) {
return mutationType, nil
}

func (c *converter) updateFullTypeDetails(schema *openapi3.SchemaRef, typeName string) (ok bool) {
var introspectionFullType *introspection.FullType
for i := 0; i < len(c.fullTypes); i++ {
if c.fullTypes[i].Name == typeName {
introspectionFullType = &c.fullTypes[i]
break
}
}

if introspectionFullType == nil {
return false
}

if !c.knownFullTypes[typeName].hasDescription {
introspectionFullType.Description = schema.Value.Description
c.knownFullTypes[typeName].hasDescription = true
}

return true
}

func ImportParsedOpenAPIv3Document(document *openapi3.T, report *operationreport.Report) *ast.Document {
c := &converter{
openapi: document,
knownFullTypes: make(map[string]struct{}),
knownFullTypes: make(map[string]*knownFullTypeDetails),
fullTypes: make([]introspection.FullType, 0),
}
data := introspection.Data{}
Expand Down Expand Up @@ -703,3 +739,11 @@ func ImportOpenAPIDocumentByte(input []byte) (*ast.Document, operationreport.Rep
func ImportOpenAPIDocumentString(input string) (*ast.Document, operationreport.Report) {
return ImportOpenAPIDocumentByte([]byte(input))
}

// checkForNewKnownFullTypeDetails will return `true` if the `openapi3.SchemaRef` contains new type details and `false` if not.
func checkForNewKnownFullTypeDetails(schema *openapi3.SchemaRef, currentDetails *knownFullTypeDetails) bool {
if !currentDetails.hasDescription && len(schema.Value.Description) > 0 {
return true
}
return false
}

0 comments on commit ec10bd5

Please sign in to comment.