-
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.
issue-665: fix example when using
json=",string"
- Loading branch information
Showing
3 changed files
with
152 additions
and
20 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
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 |
---|---|---|
@@ -1,39 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"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"` | ||
ID int `json:"id" example:"1" format:"int64"` | ||
Name string `json:"name" example:"poti"` | ||
Intvar int `json:"myint,string"` // integer as string | ||
Boolvar bool `json:",string"` // boolean as a string | ||
TrueBool bool `json:"truebool,string" example:"true"` // boolean as a string | ||
Floatvar float64 `json:",string"` // float as a string | ||
} | ||
|
||
// @Summary Call DoSomething | ||
// @Description Does something, but internal (non-exported) fields inside a struct won't be marshaled into JSON | ||
// @Description Does something | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {string} MyStruct | ||
// @Router /so-something [get] | ||
// @Param body body MyStruct true "My Struct" | ||
// @Success 200 {object} MyStruct | ||
// @Failure 500 | ||
// @Router /do-something [post] | ||
func DoSomething(c *gin.Context) { | ||
//write your code | ||
objectFromJSON := new(MyStruct) | ||
if err := c.BindJSON(&objectFromJSON); err != nil { | ||
c.AbortWithError(http.StatusInternalServerError, err) | ||
} | ||
c.JSON(http.StatusOK, objectFromJSON) | ||
} | ||
|
||
// @title Swagger Example API | ||
// @version 1.0 | ||
// @description This is a sample server. | ||
// @host localhost:4000 | ||
// @basePath /api | ||
// @basePath / | ||
func main() { | ||
r := gin.New() | ||
r.GET("/do-something", api.DoSomething) | ||
r.POST("/do-something", DoSomething) | ||
r.Run() | ||
} |