Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Apr 15, 2020
2 parents a1ce1cc + dc90151 commit a7bae01
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 2 deletions.
5 changes: 5 additions & 0 deletions parser.go
Expand Up @@ -1176,6 +1176,11 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
return &structField{name: ""}, nil
}

// Skip non-exported fields.
if !ast.IsExported(field.Names[0].Name) {
return &structField{name: ""}, nil
}

structField := &structField{
name: field.Names[0].Name,
schemaType: prop.SchemaType,
Expand Down
75 changes: 75 additions & 0 deletions parser_test.go
Expand Up @@ -1947,6 +1947,81 @@ func TestParseStructComment(t *testing.T) {
assert.Equal(t, expected, string(b))
}

func TestParseNonExportedJSONFields(t *testing.T) {
expected := `{
"swagger": "2.0",
"info": {
"description": "This is a sample server.",
"title": "Swagger Example API",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "localhost:4000",
"basePath": "/api",
"paths": {
"/so-something": {
"get": {
"description": "Does something, but internal (non-exported) fields inside a struct won't be marshaled into JSON",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Call DoSomething",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"main.MyStruct": {
"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"
}
}
}
}
}`

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, "", " ")
assert.Equal(t, expected, string(b))
}

func TestParsePetApi(t *testing.T) {
expected := `{
"schemes": [
Expand Down
39 changes: 39 additions & 0 deletions testdata/json_field_string/main.go
@@ -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()
}
44 changes: 44 additions & 0 deletions testdata/non_exported_json_fields/main.go
@@ -0,0 +1,44 @@
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"`
// not-exported variable, for internal use only, not marshaled
internal1 string
internal2 int
internal3 bool
internal4 struct {
NestedInternal 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()
}
4 changes: 2 additions & 2 deletions testdata/simple2/web/handler.go
Expand Up @@ -53,8 +53,8 @@ type Pet struct {
Hidden string `json:"-"`
UUID uuid.UUID
Decimal decimal.Decimal
customString CustomString
customStringArr []CustomString
CustomString CustomString
CustomStringArr []CustomString
NullInt sql.NullInt64 `swaggertype:"integer"`
Coeffs []big.Float `swaggertype:"array,number"`
Birthday TimestampTime `swaggertype:"primitive,integer"`
Expand Down

0 comments on commit a7bae01

Please sign in to comment.