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

@ -163,6 +163,9 @@ do-the-swag:
go install $(GOFLAGS) github.com/swaggo/swag/cmd/swag; \
fi
swag init -g pkg/routes/routes.go;
# Fix the generated swagger file, currently a workaround until swaggo can properly use go mod
sed -i '/"definitions": {/a "code.vikunja.io.web.HTTPError": {"type": "object","properties": {"code": {"type": "integer"},"message": {"type": "string"}}},' docs/docs.go;
sed -i 's/code.vikunja.io\/web.HTTPError/code.vikunja.io.web.HTTPError/g' docs/docs.go;
.PHONY: misspell-check
misspell-check:

View file

@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2018-12-02 01:35:21.999635022 +0100 CET m=+0.131973644
// 2018-12-21 22:26:31.78967283 +0100 CET m=+0.095021161
package docs
@ -71,7 +71,7 @@ var doc = `{
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -123,7 +123,7 @@ var doc = `{
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -183,14 +183,14 @@ var doc = `{
"description": "Invalid task object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -250,14 +250,14 @@ var doc = `{
"description": "Invalid list object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -304,14 +304,14 @@ var doc = `{
"description": "Invalid list object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -377,7 +377,7 @@ var doc = `{
"description": "No right to see the list.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -437,21 +437,21 @@ var doc = `{
"description": "Invalid team list object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "The team does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -517,7 +517,7 @@ var doc = `{
"description": "No right to see the list.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -577,21 +577,21 @@ var doc = `{
"description": "Invalid user list object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "The user does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -660,14 +660,14 @@ var doc = `{
"description": "The user does not have admin-access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "Team or list does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -721,14 +721,14 @@ var doc = `{
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "Team or list does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -797,14 +797,14 @@ var doc = `{
"description": "The user does not have admin-access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "User or list does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -858,14 +858,14 @@ var doc = `{
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "user or list does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -977,14 +977,14 @@ var doc = `{
"description": "Invalid namespace object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1089,14 +1089,14 @@ var doc = `{
"description": "Invalid namespace object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1148,7 +1148,7 @@ var doc = `{
"description": "The user does not have access to that namespace.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1195,14 +1195,14 @@ var doc = `{
"description": "Invalid namespace object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1329,7 +1329,7 @@ var doc = `{
"description": "No right to see the namespace.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1389,21 +1389,21 @@ var doc = `{
"description": "Invalid team namespace object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The team does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "The team does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1469,7 +1469,7 @@ var doc = `{
"description": "No right to see the namespace.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1529,21 +1529,21 @@ var doc = `{
"description": "Invalid user namespace object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "The user does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1605,14 +1605,14 @@ var doc = `{
"description": "Invalid list object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1681,14 +1681,14 @@ var doc = `{
"description": "The team does not have admin-access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "Team or namespace does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1742,14 +1742,14 @@ var doc = `{
"description": "The team does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "team or namespace does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1818,14 +1818,14 @@ var doc = `{
"description": "The user does not have admin-access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "User or namespace does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1879,14 +1879,14 @@ var doc = `{
"description": "The user does not have access to the namespace",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "user or namespace does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -1936,7 +1936,7 @@ var doc = `{
"description": "No or invalid user register object provided / User already exists.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2081,14 +2081,14 @@ var doc = `{
"description": "Invalid task object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the task (aka its list)",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2135,14 +2135,14 @@ var doc = `{
"description": "Invalid task ID provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the list",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2306,7 +2306,7 @@ var doc = `{
"description": "Invalid team object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2368,7 +2368,7 @@ var doc = `{
"description": "Invalid team object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2415,7 +2415,7 @@ var doc = `{
"description": "Invalid team object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2477,14 +2477,14 @@ var doc = `{
"description": "Invalid member object provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"403": {
"description": "The user does not have access to the team",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2576,7 +2576,7 @@ var doc = `{
"description": "User does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2626,7 +2626,7 @@ var doc = `{
"description": "Bad token provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2681,14 +2681,14 @@ var doc = `{
"description": "Something's invalid.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"404": {
"description": "User does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2738,7 +2738,7 @@ var doc = `{
"description": "Bad token provided.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2788,7 +2788,7 @@ var doc = `{
"description": "The user does not exist.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2841,7 +2841,7 @@ var doc = `{
"description": "Something's invalid.",
"schema": {
"type": "object",
"$ref": "#/definitions/code.vikunja.io/web.HTTPError"
"$ref": "#/definitions/code.vikunja.io.web.HTTPError"
}
},
"500": {
@ -2856,6 +2856,7 @@ var doc = `{
}
},
"definitions": {
"code.vikunja.io.web.HTTPError": {"type": "object","properties": {"code": {"type": "integer"},"message": {"type": "string"}}},
"models.APIUserPassword": {
"type": "object",
"properties": {
@ -3087,7 +3088,7 @@ var doc = `{
"models.PasswordTokenRequest": {
"type": "object",
"properties": {
"user_name": {
"email": {
"type": "string"
}
}

View file

@ -3074,7 +3074,7 @@
"models.PasswordTokenRequest": {
"type": "object",
"properties": {
"user_name": {
"email": {
"type": "string"
}
}

View file

@ -152,7 +152,7 @@ definitions:
type: object
models.PasswordTokenRequest:
properties:
user_name:
email:
type: string
type: object
models.Team:

2
go.mod
View file

@ -55,7 +55,7 @@ require (
github.com/swaggo/echo-swagger v0.0.0-20180315045949-97f46bb9e5a5
github.com/swaggo/files v0.0.0-20180215091130-49c8a91ea3fa // indirect
github.com/swaggo/gin-swagger v1.0.0 // indirect
github.com/swaggo/swag v1.4.1-0.20181129020348-1c8533a91397
github.com/swaggo/swag v1.4.1-0.20181210033626-0e12fd5eb026
github.com/urfave/cli v1.20.0 // indirect
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9

4
go.sum
View file

@ -140,8 +140,12 @@ github.com/swaggo/files v0.0.0-20180215091130-49c8a91ea3fa h1:194s4modF+3X3POBfG
github.com/swaggo/files v0.0.0-20180215091130-49c8a91ea3fa/go.mod h1:gxQT6pBGRuIGunNf/+tSOB5OHvguWi8Tbt82WOkf35E=
github.com/swaggo/gin-swagger v1.0.0 h1:k6Nn1jV49u+SNIWt7kejQS/iENZKZVMCNQrKOYatNF8=
github.com/swaggo/gin-swagger v1.0.0/go.mod h1:Mt37wE46iUaTAOv+HSnHbJYssKGqbS25X19lNF4YpBo=
github.com/swaggo/swag v1.4.0 h1:exX5ES4CdJWCCKmVPE+FAIN66cnHeMHU3i2SCMibBZc=
github.com/swaggo/swag v1.4.0/go.mod h1:hog2WgeMOrQ/LvQ+o1YGTeT+vWVrbi0SiIslBtxKTyM=
github.com/swaggo/swag v1.4.1-0.20181129020348-1c8533a91397 h1:xhlin3d0mSsxQlwxS+fHILT6PgG4Cmc2OZgzZL2bemI=
github.com/swaggo/swag v1.4.1-0.20181129020348-1c8533a91397/go.mod h1:hog2WgeMOrQ/LvQ+o1YGTeT+vWVrbi0SiIslBtxKTyM=
github.com/swaggo/swag v1.4.1-0.20181210033626-0e12fd5eb026 h1:XAOjF3QgjDUkVrPMO4rYvNptSHQgUlHwQsEdJOTxHQ8=
github.com/swaggo/swag v1.4.1-0.20181210033626-0e12fd5eb026/go.mod h1:hog2WgeMOrQ/LvQ+o1YGTeT+vWVrbi0SiIslBtxKTyM=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

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