Skip to content

Commit

Permalink
fix TestParseNonExportedJSONFields
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarreira committed Apr 14, 2020
1 parent 5fcb6d5 commit 6fb2a2d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 95 deletions.
95 changes: 0 additions & 95 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,6 @@ func TestParseStructComment(t *testing.T) {
}

func TestParseNonExportedJSONFields(t *testing.T) {

//region declaration
expected := `{
"swagger": "2.0",
"info": {
Expand All @@ -1962,48 +1960,6 @@ func TestParseNonExportedJSONFields(t *testing.T) {
"host": "localhost:4000",
"basePath": "/api",
"paths": {
"/posts/{post_id}": {
"get": {
"description": "get string by ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Add a new pet to the store",
"parameters": [
{
"type": "integer",
"format": "int64",
"description": "Some ID",
"name": "post_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "We need ID!!",
"schema": {
"$ref": "#/definitions/web.APIError"
}
},
"404": {
"description": "Can not find ID",
"schema": {
"$ref": "#/definitions/web.APIError"
}
}
}
}
},
"/so-something": {
"get": {
"description": "Does something, but internal (non-exported) fields inside a struct won't be marshaled into JSON",
Expand Down Expand Up @@ -2053,67 +2009,16 @@ func TestParseNonExportedJSONFields(t *testing.T) {
"example": "poti"
}
}
},
"web.APIError": {
"type": "object",
"properties": {
"createdAt": {
"description": "Error time",
"type": "string"
},
"error": {
"description": "Error an Api error",
"type": "string"
},
"errorCtx": {
"description": "Error ` + "`" + `context` + "`" + ` tick comment",
"type": "string"
},
"errorNo": {
"description": "Error ` + "`" + `number` + "`" + ` tick comment",
"type": "integer"
}
}
},
"web.Post": {
"type": "object",
"properties": {
"data": {
"description": "Post data",
"type": "object",
"properties": {
"name": {
"description": "Post tag",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"id": {
"type": "integer",
"format": "int64",
"example": 1
},
"name": {
"description": "Post name",
"type": "string",
"example": "poti"
}
}
}
}
}`
//endregion declaration

searchDir := "testdata/non_exported_json_fields"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
ioutil.WriteFile("/tmp/test1", b, 0644)
assert.Equal(t, expected, string(b))
}

Expand Down
39 changes: 39 additions & 0 deletions testdata/json_field_string/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

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

type MyStruct struct {
ID int `json:"id" example:"1" format:"int64"`
// Post name
Name string `json:"name" example:"poti"`
// Post data
Data struct {
// Post tag
Tag []string `json:"name"`
} `json:"data"`
// Integer represented by a string
MyInt int `json:"myint,string"`
}

// @Summary Call DoSomething
// @Description Does something, but internal (non-exported) fields inside a struct won't be marshaled into JSON
// @Accept json
// @Produce json
// @Success 200 {string} MyStruct
// @Router /so-something [get]
func DoSomething(c *gin.Context) {
//write your code
}

// @title Swagger Example API
// @version 1.0
// @description This is a sample server.
// @host localhost:4000
// @basePath /api
func main() {
r := gin.New()
r.GET("/do-something", api.DoSomething)
r.Run()
}

0 comments on commit 6fb2a2d

Please sign in to comment.