-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix processing alias type in struct when alias type is in another pac…
…kage (#403)
- Loading branch information
1 parent
7a85a26
commit 8e9b978
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |