Skip to content

Commit

Permalink
refactor: fix more than one models have the same the from different p…
Browse files Browse the repository at this point in the history
…ackages (#736)
  • Loading branch information
sdghchj committed Jun 14, 2020
1 parent 4a18bc9 commit c5fb1a1
Show file tree
Hide file tree
Showing 17 changed files with 1,503 additions and 2,145 deletions.
1 change: 1 addition & 0 deletions go.mod
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
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

0 comments on commit c5fb1a1

Please sign in to comment.