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

fix: only execute 'go list' when enable 'parseDependency' flag #439

Merged
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
2 changes: 2 additions & 0 deletions example/basic/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/swag/example/basic/web"
)

// GetStringByInt example
Expand All @@ -17,6 +18,7 @@ import (
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /testapi/get-string-by-int/{some_id} [get]
func GetStringByInt(c *gin.Context) {
_ := web.Pet{}
//write your code
}

Expand Down
54 changes: 28 additions & 26 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,14 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
return err
}

pkgName, err := getPkgName(path.Dir(absMainAPIFilePath))
if err != nil {
return err
}

if err := t.Resolve(pkgName); err != nil {
return fmt.Errorf("pkg %s cannot find all dependencies, %s", pkgName, err)
}

if parser.ParseDependency {
pkgName, err := getPkgName(path.Dir(absMainAPIFilePath))
if err != nil {
return err
}
if err := t.Resolve(pkgName); err != nil {
return fmt.Errorf("pkg %s cannot find all dependencies, %s", pkgName, err)
}
for i := 0; i < len(t.Root.Deps); i++ {
if err := parser.getAllGoFileInfoFromDeps(&t.Root.Deps[i]); err != nil {
return err
Expand Down Expand Up @@ -1016,27 +1014,31 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) (map
}

typeSpec := parser.TypeDefinitions[pkgName][typeName]
schema, err := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
if err != nil {
return properties, []string{}, err
}
schemaType := "unknown"
if len(schema.SchemaProps.Type) > 0 {
schemaType = schema.SchemaProps.Type[0]
}
if typeSpec != nil {
schema, err := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
if err != nil {
return properties, []string{}, err
}
schemaType := "unknown"
if len(schema.SchemaProps.Type) > 0 {
schemaType = schema.SchemaProps.Type[0]
}

switch schemaType {
case "object":
for k, v := range schema.SchemaProps.Properties {
properties[k] = v
switch schemaType {
case "object":
for k, v := range schema.SchemaProps.Properties {
properties[k] = v
}
case "array":
properties[typeName] = schema
default:
Printf("Can't extract properties from a schema of type '%s'", schemaType)
}
case "array":
properties[typeName] = schema
default:
Printf("Can't extract properties from a schema of type '%s'", schemaType)

return properties, schema.SchemaProps.Required, nil
}

return properties, schema.SchemaProps.Required, nil
return properties, nil, nil
}

func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
Expand Down
11 changes: 11 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2641,3 +2641,14 @@ func TestFixes432(t *testing.T) {
t.Error("Failed to ignore valid pkg: " + err.Error())
}
}

func TestParseOutsideDependencies(t *testing.T) {
searchDir := "testdata/pare_outside_dependencies"
mainAPIFile := "cmd/main.go"

p := New()
p.ParseDependency = true
if err := p.ParseAPI(searchDir, mainAPIFile); err != nil {
t.Error("Failed to parse api: " + err.Error())
}
}
30 changes: 30 additions & 0 deletions testdata/pare_outside_dependencies/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/swag/example/basic/api"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2
func main() {
_ := web.Pet{}
r := gin.New()
r.GET("/testapi/get-string-by-int/:some_id", api.GetStringByInt)
r.GET("//testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
r.POST("/testapi/upload", api.Upload)
r.Run()

}