1
0

Fix swaggerdocs generation to produce actually valid output (#39)

This commit is contained in:
konrad
2018-12-21 21:30:43 +00:00
committed by Gitea
parent 0edc5fd315
commit 7322bfafb3
10 changed files with 104 additions and 90 deletions

View File

@ -8,6 +8,7 @@ GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOLIST=$(GOCMD) list
BINARY_NAME=swag
PACKAGES=$(shell $(GOLIST) -f {{.Dir}} ./... | grep -v /example)
all: test build
@ -21,16 +22,8 @@ clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
DIRS=$(shell $(GOLIST) -f {{.Dir}} ./...)
lint:
@for d in $(DIRS) ; do \
if [ "`$(GOIMPORT) -l $$d/*.go | tee /dev/stderr`" ]; then \
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
fi \
done
@if [ "`$(GOLINT) ./... | grep -vf .golint_exclude | tee /dev/stderr`" ]; then \
echo "^ - Lint errors!" && echo && exit 1; \
fi
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
deps:
$(GOGET) -v ./...

View File

@ -70,6 +70,7 @@ $ swag init
- [gin](http://github.com/swaggo/gin-swagger)
- [echo](http://github.com/swaggo/echo-swagger)
- [buffalo](https://github.com/swaggo/buffalo-swagger)
- [net/http](https://github.com/swaggo/http-swagger)
## How to use it with Gin
@ -176,8 +177,8 @@ import (
func main() {
// programatically set swagger info
docs.Title = "Swagger Example API"
docs.Description = "This is a sample server Petstore server."
docs.SwaggerInfo.Title = "Swagger Example API"
docs.SwaggerInfo.Description = "This is a sample server Petstore server."
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "petstore.swagger.io"
docs.SwaggerInfo.BasePath = "/v2"

View File

@ -432,7 +432,7 @@ func (parser *Parser) ParseDefinition(pkgName, typeName string, typeSpec *ast.Ty
parser.structStack = append(parser.structStack, refTypeName)
log.Println("Generating " + refTypeName)
parser.swagger.Definitions[refTypeName] = parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
parser.swagger.Definitions[refTypeName] = parser.parseTypeExpr(pkgName, typeName, typeSpec.Type, true)
}
func (parser *Parser) collectRequiredFields(pkgName string, properties map[string]spec.Schema) (requiredFields []string) {
@ -457,7 +457,6 @@ func (parser *Parser) collectRequiredFields(pkgName string, properties map[strin
}
if tname != "object" {
requiredFields = append(requiredFields, prop.SchemaProps.Required...)
prop.SchemaProps.Required = make([]string, 0)
}
properties[k] = prop
}
@ -474,7 +473,7 @@ func fullTypeName(pkgName, typeName string) string {
// parseTypeExpr parses given type expression that corresponds to the type under
// given name and package, and returns swagger schema for it.
func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr) spec.Schema {
func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr, flattenRequired bool) spec.Schema {
switch expr := typeExpr.(type) {
// type Foo struct {...}
case *ast.StructType:
@ -497,11 +496,24 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)
}
}
required := parser.collectRequiredFields(pkgName, properties)
// unset required from properties because we've aggregated them
if flattenRequired {
for k, prop := range properties {
tname := prop.SchemaProps.Type[0]
if tname != "object" {
prop.SchemaProps.Required = make([]string, 0)
}
properties[k] = prop
}
}
return spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: properties,
Required: parser.collectRequiredFields(pkgName, properties),
Required: required,
},
}
@ -516,11 +528,11 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)
// type Foo *Baz
case *ast.StarExpr:
return parser.parseTypeExpr(pkgName, typeName, expr.X)
return parser.parseTypeExpr(pkgName, typeName, expr.X, true)
// type Foo []Baz
case *ast.ArrayType:
itemSchema := parser.parseTypeExpr(pkgName, "", expr.Elt)
itemSchema := parser.parseTypeExpr(pkgName, "", expr.Elt, true)
return spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"array"},
@ -734,7 +746,7 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) map[
}
typeSpec := parser.TypeDefinitions[pkgName][typeName]
schema := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
schema := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type, false)
schemaType := "unknown"
if len(schema.SchemaProps.Type) > 0 {

2
vendor/modules.txt vendored
View File

@ -139,7 +139,7 @@ github.com/stretchr/testify/assert
github.com/swaggo/echo-swagger
# github.com/swaggo/files v0.0.0-20180215091130-49c8a91ea3fa
github.com/swaggo/files
# github.com/swaggo/swag v1.4.1-0.20181129020348-1c8533a91397
# github.com/swaggo/swag v1.4.1-0.20181210033626-0e12fd5eb026
github.com/swaggo/swag/cmd/swag
github.com/swaggo/swag
github.com/swaggo/swag/gen