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

Refactor3: fix more than one models have the same name the from different packages #736

Merged
merged 7 commits into from
Jun 14, 2020
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-openapi/jsonreference v0.19.3
github.com/go-openapi/spec v0.19.4
github.com/satori/go.uuid v1.2.0
github.com/shopspring/decimal v1.2.0
github.com/stretchr/testify v1.4.0
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0
Expand Down
67 changes: 8 additions & 59 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ var mimeTypePattern = regexp.MustCompile("^[^/]+/[^/]+$")

// NewOperation creates a new Operation with default properties.
// map[int]Response
func NewOperation() *Operation {
func NewOperation(parser *Parser) *Operation {
if parser == nil {
parser = New()
}
return &Operation{
parser: parser,
HTTPMethod: "get",
Operation: spec.Operation{
OperationProps: spec.OperationProps{},
Expand Down Expand Up @@ -184,16 +188,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
},
}
case OBJECT:
refType, typeSpec, err := operation.registerSchemaType(refType, astFile)
if err != nil {
return err
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
refSplit := strings.Split(refType, ".")
schema, err := operation.parser.parseStruct(refSplit[0], structType.Fields)
schema, err := operation.parser.getTypeSchema(refType, astFile, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -276,52 +271,6 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
return nil
}

func (operation *Operation) registerSchemaType(schemaType string, astFile *ast.File) (string, *ast.TypeSpec, error) {
if !strings.ContainsRune(schemaType, '.') {
if astFile == nil {
return schemaType, nil, fmt.Errorf("no package name for type %s", schemaType)
}
schemaType = fullTypeName(astFile.Name.String(), schemaType)
}
refSplit := strings.Split(schemaType, ".")
pkgName := refSplit[0]
typeName := refSplit[1]
if typeSpec, ok := operation.parser.TypeDefinitions[pkgName][typeName]; ok {
operation.parser.registerTypes[schemaType] = typeSpec
return schemaType, typeSpec, nil
}
var typeSpec *ast.TypeSpec
if astFile == nil {
return schemaType, nil, fmt.Errorf("can not register schema type: %q reason: astFile == nil", schemaType)
}
for _, imp := range astFile.Imports {
if imp.Name != nil && imp.Name.Name == pkgName { // the import had an alias that matched
break
}
impPath := strings.Replace(imp.Path.Value, `"`, ``, -1)
if strings.HasSuffix(impPath, "/"+pkgName) {
var err error
typeSpec, err = findTypeDef(impPath, typeName)
if err != nil {
return schemaType, nil, fmt.Errorf("can not find type def: %q error: %s", schemaType, err)
}
break
}
}

if typeSpec == nil {
return schemaType, nil, fmt.Errorf("can not find schema type: %q", schemaType)
}

if _, ok := operation.parser.TypeDefinitions[pkgName]; !ok {
operation.parser.TypeDefinitions[pkgName] = make(map[string]*ast.TypeSpec)
}

operation.parser.TypeDefinitions[pkgName][typeName] = typeSpec
operation.parser.registerTypes[schemaType] = typeSpec
return schemaType, typeSpec, nil
}

var regexAttributes = map[string]*regexp.Regexp{
// for Enums(A, B)
"enums": regexp.MustCompile(`(?i)\s+enums\(.*\)`),
Expand Down Expand Up @@ -654,11 +603,11 @@ func (operation *Operation) parseObjectSchema(refType string, astFile *ast.File)
return operation.parseCombinedObjectSchema(refType, astFile)
default:
if operation.parser != nil { // checking refType has existing in 'TypeDefinitions'
refNewType, typeSpec, err := operation.registerSchemaType(refType, astFile)
schema, err := operation.parser.getTypeSchema(refType, astFile, true)
if err != nil {
return nil, err
}
refType = TypeDocName(refNewType, typeSpec)
return schema, nil
}

return RefSchema(refType), nil
Expand Down
Loading