Skip to content
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
6 changes: 3 additions & 3 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func DescribeParameters(params openapi3.Parameters, path []string) ([]ParameterD
// If this is a reference to a predefined type, simply use the reference
// name as the type. $ref: "#/components/schemas/custom_type" becomes
// "CustomType".
if paramOrRef.Ref != "" {
if IsGoTypeReference(paramOrRef.Ref) {
goType, err := RefPathToGoType(paramOrRef.Ref)
if err != nil {
return nil, fmt.Errorf("error dereferencing (%s) for param (%s): %s",
Expand Down Expand Up @@ -297,7 +297,7 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]TypeDefinition, er
Schema: responseSchema,
ResponseName: responseName,
}
if contentType.Schema.Ref != "" {
if IsGoTypeReference(contentType.Schema.Ref) {
refType, err := RefPathToGoType(contentType.Schema.Ref)
if err != nil {
return nil, errors.Wrap(err, "error dereferencing response Ref")
Expand Down Expand Up @@ -517,7 +517,7 @@ func GenerateBodyDefinitions(operationID string, bodyOrRef *openapi3.RequestBody
}

// If the body is a pre-defined type
if bodyOrRef.Ref != "" {
if IsGoTypeReference(bodyOrRef.Ref) {
// Convert the reference path to Go type
refType, err := RefPathToGoType(bodyOrRef.Ref)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {

schema := sref.Value

if sref.Ref != "" {
if IsGoTypeReference(sref.Ref) {
var err error
// Convert the reference path to Go type
refType, err = RefPathToGoType(sref.Ref)
Expand Down Expand Up @@ -380,7 +380,7 @@ func MergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) {

var refType string
var err error
if ref != "" {
if IsGoTypeReference(ref) {
refType, err = RefPathToGoType(ref)
if err != nil {
return Schema{}, errors.Wrap(err, "error converting reference path to a go type")
Expand Down Expand Up @@ -433,7 +433,7 @@ func GenStructFromAllOf(allOf []*openapi3.SchemaRef, path []string) (string, err
objectParts := []string{"struct {"}
for _, schemaOrRef := range allOf {
ref := schemaOrRef.Ref
if ref != "" {
if IsGoTypeReference(ref) {
// We have a referenced type, we will generate an inlined struct
// member.
// struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ func RefPathToGoType(refPath string) (string, error) {
}
}

// This function takes a $ref value and checks if it has link to go type.
// #/components/schemas/Foo -> true
// ./local/file.yml#/components/parameters/Bar -> true
// ./local/file.yml -> false
// The function can be used to check whether RefPathToGoType($ref) is possible.
//
func IsGoTypeReference(ref string) bool {
return ref != "" && !IsWholeDocumentReference(ref)
}

// This function takes a $ref value and checks if it is whole document reference.
// #/components/schemas/Foo -> false
// ./local/file.yml#/components/parameters/Bar -> false
// ./local/file.yml -> true
// http://deepmap.com/schemas/document.json -> true
// http://deepmap.com/schemas/document.json#/Foo -> false
//
func IsWholeDocumentReference(ref string) bool {
return ref != "" && !strings.ContainsAny(ref, "#")
}

// This function converts a swagger style path URI with parameters to a
// Echo compatible path URI. We need to replace all of Swagger parameters with
// ":param". Valid input parameters are:
Expand Down
20 changes: 20 additions & 0 deletions pkg/codegen/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ func TestRefPathToGoType(t *testing.T) {
assert.Errorf(t, err, "Expected an error on reference depth")
}

func TestIsWholeDocumentReference(t *testing.T) {
assert.Equal(t, false, IsWholeDocumentReference(""))
assert.Equal(t, false, IsWholeDocumentReference("#/components/schemas/Foo"))
assert.Equal(t, false, IsWholeDocumentReference("doc.json#/components/schemas/Foo"))
assert.Equal(t, true, IsWholeDocumentReference("doc.json"))
assert.Equal(t, true, IsWholeDocumentReference("../doc.json"))
assert.Equal(t, false, IsWholeDocumentReference("http://deepmap.com/doc.json#/components/parameters/foo_bar"))
assert.Equal(t, true, IsWholeDocumentReference("http://deepmap.com/doc.json"))
}

func TestIsGoTypeReference(t *testing.T) {
assert.Equal(t, false, IsGoTypeReference(""))
assert.Equal(t, true, IsGoTypeReference("#/components/schemas/Foo"))
assert.Equal(t, true, IsGoTypeReference("doc.json#/components/schemas/Foo"))
assert.Equal(t, false, IsGoTypeReference("doc.json"))
assert.Equal(t, false, IsGoTypeReference("../doc.json"))
assert.Equal(t, true, IsGoTypeReference("http://deepmap.com/doc.json#/components/parameters/foo_bar"))
assert.Equal(t, false, IsGoTypeReference("http://deepmap.com/doc.json"))
}

func TestSwaggerUriToEchoUri(t *testing.T) {
assert.Equal(t, "/path", SwaggerUriToEchoUri("/path"))
assert.Equal(t, "/path/:arg", SwaggerUriToEchoUri("/path/{arg}"))
Expand Down