From 8e9b9781562b2c19ff09ddc2a57398d204f184bd Mon Sep 17 00:00:00 2001 From: Andrey Volkov Date: Sat, 1 Jun 2019 05:29:49 +0300 Subject: [PATCH] Fix processing alias type in struct when alias type is in another package (#403) --- parser_test.go | 70 ++++++++++++++++++++++++++++++ property.go | 5 +++ testdata/alias_type/api/api.go | 52 ++++++++++++++++++++++ testdata/alias_type/data/alias.go | 12 +++++ testdata/alias_type/main.go | 26 +++++++++++ testdata/alias_type/types/alias.go | 7 +++ 6 files changed, 172 insertions(+) create mode 100644 testdata/alias_type/api/api.go create mode 100644 testdata/alias_type/data/alias.go create mode 100644 testdata/alias_type/main.go create mode 100644 testdata/alias_type/types/alias.go diff --git a/parser_test.go b/parser_test.go index 70c324910..e59af253a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -2127,6 +2127,76 @@ func TestParseModelNotUnderRoot(t *testing.T) { assert.Equal(t, expected, string(b)) } +func TestParseModelAsTypeAlias(t *testing.T) { + expected := `{ + "swagger": "2.0", + "info": { + "description": "This is a sample server Petstore server.", + "title": "Swagger Example API", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "API Support", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0" + }, + "host": "petstore.swagger.io", + "basePath": "/v2", + "paths": { + "/testapi/time-as-time-container": { + "get": { + "description": "test container with time and time alias", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "summary": "Get container with time and time alias", + "operationId": "time-as-time-container", + "responses": { + "200": { + "description": "ok", + "schema": { + "type": "object", + "$ref": "#/definitions/data.TimeContainer" + } + } + } + } + } + }, + "definitions": { + "data.TimeContainer": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "name": { + "type": "string" + }, + "timestamp": { + "type": "string" + } + } + } + } +}` + searchDir := "testdata/alias_type" + mainAPIFile := "main.go" + p := New() + p.ParseAPI(searchDir, mainAPIFile) + + b, _ := json.MarshalIndent(p.swagger, "", " ") + assert.Equal(t, expected, string(b)) +} + func TestParseComposition(t *testing.T) { searchDir := "testdata/composition" mainAPIFile := "main.go" diff --git a/property.go b/property.go index bbc2a5982..11e028213 100644 --- a/property.go +++ b/property.go @@ -52,6 +52,11 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr, parser *Parse if pkgName, ok := astTypeSelectorExpr.X.(*ast.Ident); ok { if typeDefinitions, ok := parser.TypeDefinitions[pkgName.Name][astTypeSelectorExpr.Sel.Name]; ok { + if expr, ok := typeDefinitions.Type.(*ast.SelectorExpr); ok { + if primitiveType, err := convertFromSpecificToPrimitive(expr.Sel.Name); err == nil { + return propertyNewFunc(primitiveType, "") + } + } parser.ParseDefinition(pkgName.Name, astTypeSelectorExpr.Sel.Name, typeDefinitions) return propertyNewFunc(astTypeSelectorExpr.Sel.Name, pkgName.Name) } diff --git a/testdata/alias_type/api/api.go b/testdata/alias_type/api/api.go new file mode 100644 index 000000000..1d6199436 --- /dev/null +++ b/testdata/alias_type/api/api.go @@ -0,0 +1,52 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "github.com/swaggo/swag/testdata/alias_type/data" + "log" + "time" +) + +/*// @Summary Get time as string +// @Description get time as string +// @ID time-as-string +// @Accept json +// @Produce json +// @Success 200 {object} data.StringAlias "ok" +// @Router /testapi/time-as-string [get] +func GetTimeAsStringAlias(c *gin.Context) { + var foo data.StringAlias = "test" + log.Println(foo) + //write your code +}*/ + +/*// @Summary Get time as time +// @Description get time as time +// @ID time-as-time +// @Accept json +// @Produce json +// @Success 200 {object} data.DateOnly "ok" +// @Router /testapi/time-as-time [get] +func GetTimeAsTimeAlias(c *gin.Context) { + var foo = data.DateOnly(time.Now()) + log.Println(foo) + //write your code +}*/ + +// @Summary Get container with time and time alias +// @Description test container with time and time alias +// @ID time-as-time-container +// @Accept json +// @Produce json +// @Success 200 {object} data.TimeContainer "ok" +// @Router /testapi/time-as-time-container [get] +func GetTimeAsTimeContainer(c *gin.Context) { + now := time.Now() + var foo = data.TimeContainer{ + Name: "test", + Timestamp: now, + //CreatedAt: &now, + } + log.Println(foo) + //write your code +} diff --git a/testdata/alias_type/data/alias.go b/testdata/alias_type/data/alias.go new file mode 100644 index 000000000..24051055f --- /dev/null +++ b/testdata/alias_type/data/alias.go @@ -0,0 +1,12 @@ +package data + +import ( + "github.com/swaggo/swag/testdata/alias_type/types" + "time" +) + +type TimeContainer struct { + Name types.StringAlias `json:"name"` + Timestamp time.Time `json:"timestamp"` + CreatedAt types.DateOnly `json:"created_at"` +} diff --git a/testdata/alias_type/main.go b/testdata/alias_type/main.go new file mode 100644 index 000000000..94affaf99 --- /dev/null +++ b/testdata/alias_type/main.go @@ -0,0 +1,26 @@ +package alias_type + +import ( + "github.com/gin-gonic/gin" + "github.com/swaggo/swag/testdata/alias_type/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 support@swagger.io + +// @license.name Apache 2.0 +// @license.url http://www.apache.org/licenses/LICENSE-2.0.html + +// @host petstore.swagger.io +// @BasePath /v2 +func main() { + r := gin.New() + r.GET("/testapi/time-as-time-container", api.GetTimeAsTimeContainer) + r.Run() +} diff --git a/testdata/alias_type/types/alias.go b/testdata/alias_type/types/alias.go new file mode 100644 index 000000000..cd3690769 --- /dev/null +++ b/testdata/alias_type/types/alias.go @@ -0,0 +1,7 @@ +package types + +import "time" + +type StringAlias string + +type DateOnly time.Time