Skip to content

Commit

Permalink
Added unit test for cgo package import
Browse files Browse the repository at this point in the history
  • Loading branch information
maitredede committed Apr 9, 2020
1 parent b9554c4 commit 1e6c432
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,29 @@ func TestGen_GeneratedDoc(t *testing.T) {
os.Remove(expectedFile)
}
}

func TestGen_cgoImports(t *testing.T) {
searchDir := "../testdata/simple_cgo"

config := &Config{
SearchDir: searchDir,
MainAPIFile: "./main.go",
OutputDir: "../testdata/simple_cgo/docs",
PropNamingStrategy: "",
ParseDependency: true,
}

assert.NoError(t, New().Build(config))

expectedFiles := []string{
path.Join(config.OutputDir, "docs.go"),
path.Join(config.OutputDir, "swagger.json"),
path.Join(config.OutputDir, "swagger.yaml"),
}
for _, expectedFile := range expectedFiles {
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Fatal(err)
}
os.Remove(expectedFile)
}
}
116 changes: 116 additions & 0 deletions testdata/simple_cgo/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package api

import (
"github.com/gin-gonic/gin"
)

// @Summary Add a new pet to the store
// @Description get string by ID
// @ID get-string-by-int
// @Accept json
// @Produce json
// @Param some_id path int true "Some ID" Format(int64)
// @Param some_id body web.Pet true "Some ID"
// @Success 200 {string} string "ok"
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /testapi/get-string-by-int/{some_id} [get]
func GetStringByInt(c *gin.Context) {
//write your code
}

// @Description get struct array by ID
// @ID get-struct-array-by-string
// @Accept json
// @Produce json
// @Param some_id path string true "Some ID"
// @Param category query int true "Category" Enums(1, 2, 3)
// @Param offset query int true "Offset" Mininum(0) default(0)
// @Param limit query int true "Limit" Maxinum(50) default(10)
// @Param q query string true "q" Minlength(1) Maxlength(50) default("")
// @Success 200 {string} string "ok"
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Security ApiKeyAuth
// @Security BasicAuth
// @Security OAuth2Application[write]
// @Security OAuth2Implicit[read, admin]
// @Security OAuth2AccessCode[read]
// @Security OAuth2Password[admin]
// @Router /testapi/get-struct-array-by-string/{some_id} [get]
func GetStructArrayByString(c *gin.Context) {
//write your code
}

// @Summary Upload file
// @Description Upload file
// @ID file.upload
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "this is a test file"
// @Success 200 {string} string "ok"
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 401 {array} string
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /file/upload [post]
func Upload(ctx *gin.Context) {
//write your code
}

// @Summary use Anonymous field
// @Success 200 {object} web.RevValue "ok"
func AnonymousField() {

}

// @Summary use pet2
// @Success 200 {object} web.Pet2 "ok"
func Pet2() {

}

// @Summary Use IndirectRecursiveTest
// @Success 200 {object} web.IndirectRecursiveTest
func IndirectRecursiveTest() {
}

// @Summary Use Tags
// @Success 200 {object} web.Tags
func Tags() {
}

// @Summary Use CrossAlias
// @Success 200 {object} web.CrossAlias
func CrossAlias() {
}

// @Summary Use AnonymousStructArray
// @Success 200 {object} web.AnonymousStructArray
func AnonymousStructArray() {
}

type Pet3 struct {
ID int `json:"id"`
}

// @Success 200 {object} web.Pet5a "ok"
func GetPet5a() {

}

// @Success 200 {object} web.Pet5b "ok"
func GetPet5b() {

}

// @Success 200 {object} web.Pet5c "ok"
func GetPet5c() {

}

type SwagReturn []map[string]string

// @Success 200 {object} api.SwagReturn "ok"
func GetPet6MapString() {

}
6 changes: 6 additions & 0 deletions testdata/simple_cgo/cross/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cross

type Cross struct {
Array []string
String string
}
66 changes: 66 additions & 0 deletions testdata/simple_cgo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

/*
#include <stdio.h>
void Hello(){
printf("Hello world\n");
}
*/
import "C"

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/swag/testdata/simple/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

// @securityDefinitions.basic BasicAuth

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization

// @securitydefinitions.oauth2.application OAuth2Application
// @tokenUrl https://example.com/oauth/token
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.implicit OAuth2Implicit
// @authorizationurl https://example.com/oauth/authorize
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.password OAuth2Password
// @tokenUrl https://example.com/oauth/token
// @scope.read Grants read access
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.accessCode OAuth2AccessCode
// @tokenUrl https://example.com/oauth/token
// @authorizationurl https://example.com/oauth/authorize
// @scope.admin Grants read and write access to administrative information
func main() {
C.Hello()

r := gin.New()
r.GET("/testapi/get-string-by-int/:some_id", api.GetStringByInt)
r.GET("/testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
r.POST("/testapi/upload", api.Upload)
r.Run()
}
97 changes: 97 additions & 0 deletions testdata/simple_cgo/web/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package web

import (
"time"

"github.com/satori/go.uuid"
"github.com/shopspring/decimal"
"github.com/swaggo/swag/testdata/simple/cross"
)

type Pet struct {
ID int `json:"id" example:"1" format:"int64" readonly:"true"`
Category struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"category_name"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" format:"url"`
SmallCategory struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"detail_category_name" binding:"required" minLength:"4" maxLength:"16"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
} `json:"small_category"`
} `json:"category"`
Name string `json:"name" example:"poti" binding:"required"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" binding:"required"`
Tags []Tag `json:"tags"`
Pets *[]Pet2 `json:"pets"`
Pets2 []*Pet2 `json:"pets2"`
Status string `json:"status" enums:"healthy,ill"`
Price float32 `json:"price" example:"3.25" minimum:"1.0" maximum:"1000"`
IsAlive bool `json:"is_alive" example:"true" default:"true"`
Data interface{} `json:"data"`
Hidden string `json:"-"`
UUID uuid.UUID `json:"uuid"`
Decimal decimal.Decimal `json:"decimal"`
IntArray []int `json:"int_array" example:"1,2"`
EnumArray []int `json:"enum_array" enums:"1,2,3,5,7"`
}

type Tag struct {
ID int `json:"id" format:"int64"`
Name string `json:"name"`
Pets []Pet `json:"pets"`
}

type Tags []*Tag

type AnonymousStructArray []struct {
Foo string `json:"foo"`
}

type CrossAlias cross.Cross

type Pet2 struct {
ID int `json:"id"`
MiddleName *string `json:"middlename" extensions:"x-nullable,x-abc=def"`
DeletedAt *time.Time `json:"deleted_at"`
}

type IndirectRecursiveTest struct {
Tags []Tag
}

type APIError struct {
ErrorCode int
ErrorMessage string
CreatedAt time.Time
}

type RevValueBase struct {
Status bool `json:"Status"`

Err int32 `json:"Err,omitempty"`
}
type RevValue struct {
RevValueBase `json:"rev_value_base"`

Data int `json:"Data"`
Cross cross.Cross `json:"cross"`
Crosses []cross.Cross `json:"crosses"`
}

// Below we have Pet5b as base type and Pet5a and Pet5c both have Pet5b as anonymous field, inheriting it's properties
// By using these names we ensure that our test will fill if the order of parsing matters at all

type Pet5a struct {
*Pet5b
Odd bool `json:"odd" binding:"required"`
}

type Pet5b struct {
Name string `json:"name" binding:"required"`
}

type Pet5c struct {
*Pet5b
Odd bool `json:"odd" binding:"required"`
}

0 comments on commit 1e6c432

Please sign in to comment.