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 processing alias type in struct when alias type is in another package #403

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
70 changes: 70 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]"
},
"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"
Expand Down
5 changes: 5 additions & 0 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
52 changes: 52 additions & 0 deletions testdata/alias_type/api/api.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions testdata/alias_type/data/alias.go
Original file line number Diff line number Diff line change
@@ -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"`
}
26 changes: 26 additions & 0 deletions testdata/alias_type/main.go
Original file line number Diff line number Diff line change
@@ -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 [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() {
r := gin.New()
r.GET("/testapi/time-as-time-container", api.GetTimeAsTimeContainer)
r.Run()
}
7 changes: 7 additions & 0 deletions testdata/alias_type/types/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

import "time"

type StringAlias string

type DateOnly time.Time