Skip to content

Commit c9056f0

Browse files
Arrimeasonlin404
andauthored
issue-503: added type map in example value (#683)
* issue-503: added type map in example value * Refactor and update example tag for map(object) * Add data in test * Add test * Add test Co-authored-by: Eason Lin <[email protected]>
1 parent 09f9621 commit c9056f0

File tree

15 files changed

+547
-15
lines changed

15 files changed

+547
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package controller
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
// GetMap godoc
6+
// @Summary Get Map Example
7+
// @Description get map
8+
// @ID get-map
9+
// @Accept json
10+
// @Produce json
11+
// @Success 200 {object} Response
12+
// @Router /test [get]
13+
func (c *Controller) GetMap(ctx *gin.Context) {
14+
ctx.JSON(200, Response{
15+
Title: map[string]string{
16+
"en": "Map",
17+
},
18+
CustomType: map[string]interface{}{
19+
"key": "value",
20+
},
21+
Object: Data{
22+
Text: "object text",
23+
},
24+
})
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package controller
2+
3+
// Controller example
4+
type Controller struct {
5+
}
6+
7+
// NewController example
8+
func NewController() *Controller {
9+
return &Controller{}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package controller
2+
3+
type Response struct {
4+
Title map[string]string `json:"title" example:"en:Map,ru:Карта,kk:Карталар"`
5+
CustomType map[string]interface{} `json:"map_data" swaggertype:"object,string" example:"key:value,key2:value2"`
6+
Object Data `json:"object"`
7+
}
8+
9+
type Data struct {
10+
Text string `json:"title" example:"Object data"`
11+
}
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
2+
// This file was generated by swaggo/swag
3+
4+
package docs
5+
6+
import (
7+
"bytes"
8+
"encoding/json"
9+
"strings"
10+
11+
"github.com/alecthomas/template"
12+
"github.com/swaggo/swag"
13+
)
14+
15+
var doc = `{
16+
"schemes": {{ marshal .Schemes }},
17+
"swagger": "2.0",
18+
"info": {
19+
"description": "{{.Description}}",
20+
"title": "{{.Title}}",
21+
"termsOfService": "http://swagger.io/terms/",
22+
"contact": {},
23+
"license": {
24+
"name": "Apache 2.0",
25+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
26+
},
27+
"version": "{{.Version}}"
28+
},
29+
"host": "{{.Host}}",
30+
"basePath": "{{.BasePath}}",
31+
"paths": {
32+
"/test": {
33+
"get": {
34+
"description": "get map",
35+
"consumes": [
36+
"application/json"
37+
],
38+
"produces": [
39+
"application/json"
40+
],
41+
"summary": "Get Map Example",
42+
"operationId": "get-map",
43+
"responses": {
44+
"200": {
45+
"description": "OK",
46+
"schema": {
47+
"$ref": "#/definitions/controller.Response"
48+
}
49+
}
50+
}
51+
}
52+
}
53+
},
54+
"definitions": {
55+
"controller.Data": {
56+
"type": "object",
57+
"properties": {
58+
"title": {
59+
"type": "string",
60+
"example": "Object data"
61+
}
62+
}
63+
},
64+
"controller.Response": {
65+
"type": "object",
66+
"properties": {
67+
"map_data": {
68+
"type": "object",
69+
"additionalProperties": {
70+
"type": "string"
71+
},
72+
"example": {
73+
"key": "value",
74+
"key2": "value2"
75+
}
76+
},
77+
"object": {
78+
"$ref": "#/definitions/controller.Data"
79+
},
80+
"title": {
81+
"type": "object",
82+
"additionalProperties": {
83+
"type": "string"
84+
},
85+
"example": {
86+
"en": "Map",
87+
"kk": "Карталар",
88+
"ru": "Карта"
89+
}
90+
}
91+
}
92+
}
93+
}
94+
}`
95+
96+
type swaggerInfo struct {
97+
Version string
98+
Host string
99+
BasePath string
100+
Schemes []string
101+
Title string
102+
Description string
103+
}
104+
105+
// SwaggerInfo holds exported Swagger Info so clients can modify it
106+
var SwaggerInfo = swaggerInfo{
107+
Version: "1.0",
108+
Host: "localhost:8080",
109+
BasePath: "/api/v1",
110+
Schemes: []string{},
111+
Title: "Swagger Map Example API",
112+
Description: "",
113+
}
114+
115+
type s struct{}
116+
117+
func (s *s) ReadDoc() string {
118+
sInfo := SwaggerInfo
119+
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
120+
121+
t, err := template.New("swagger_info").Funcs(template.FuncMap{
122+
"marshal": func(v interface{}) string {
123+
a, _ := json.Marshal(v)
124+
return string(a)
125+
},
126+
}).Parse(doc)
127+
if err != nil {
128+
return doc
129+
}
130+
131+
var tpl bytes.Buffer
132+
if err := t.Execute(&tpl, sInfo); err != nil {
133+
return doc
134+
}
135+
136+
return tpl.String()
137+
}
138+
139+
func init() {
140+
swag.Register(swag.Name, &s{})
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"title": "Swagger Map Example API",
5+
"termsOfService": "http://swagger.io/terms/",
6+
"contact": {},
7+
"license": {
8+
"name": "Apache 2.0",
9+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
10+
},
11+
"version": "1.0"
12+
},
13+
"host": "localhost:8080",
14+
"basePath": "/api/v1",
15+
"paths": {
16+
"/test": {
17+
"get": {
18+
"description": "get map",
19+
"consumes": [
20+
"application/json"
21+
],
22+
"produces": [
23+
"application/json"
24+
],
25+
"summary": "Get Map Example",
26+
"operationId": "get-map",
27+
"responses": {
28+
"200": {
29+
"description": "OK",
30+
"schema": {
31+
"$ref": "#/definitions/controller.Response"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"definitions": {
39+
"controller.Data": {
40+
"type": "object",
41+
"properties": {
42+
"title": {
43+
"type": "string",
44+
"example": "Object data"
45+
}
46+
}
47+
},
48+
"controller.Response": {
49+
"type": "object",
50+
"properties": {
51+
"map_data": {
52+
"type": "object",
53+
"additionalProperties": {
54+
"type": "string"
55+
},
56+
"example": {
57+
"key": "value",
58+
"key2": "value2"
59+
}
60+
},
61+
"object": {
62+
"$ref": "#/definitions/controller.Data"
63+
},
64+
"title": {
65+
"type": "object",
66+
"additionalProperties": {
67+
"type": "string"
68+
},
69+
"example": {
70+
"en": "Map",
71+
"kk": "Карталар",
72+
"ru": "Карта"
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
basePath: /api/v1
2+
definitions:
3+
controller.Data:
4+
properties:
5+
title:
6+
example: Object data
7+
type: string
8+
type: object
9+
controller.Response:
10+
properties:
11+
map_data:
12+
additionalProperties:
13+
type: string
14+
example:
15+
key: value
16+
key2: value2
17+
type: object
18+
object:
19+
$ref: '#/definitions/controller.Data'
20+
title:
21+
additionalProperties:
22+
type: string
23+
example:
24+
en: Map
25+
kk: Карталар
26+
ru: Карта
27+
type: object
28+
type: object
29+
host: localhost:8080
30+
info:
31+
contact: {}
32+
license:
33+
name: Apache 2.0
34+
url: http://www.apache.org/licenses/LICENSE-2.0.html
35+
termsOfService: http://swagger.io/terms/
36+
title: Swagger Map Example API
37+
version: "1.0"
38+
paths:
39+
/test:
40+
get:
41+
consumes:
42+
- application/json
43+
description: get map
44+
operationId: get-map
45+
produces:
46+
- application/json
47+
responses:
48+
"200":
49+
description: OK
50+
schema:
51+
$ref: '#/definitions/controller.Response'
52+
summary: Get Map Example
53+
swagger: "2.0"

example/object-map-example/go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/swaggo/swag/example/object-map-example
2+
3+
go 1.14
4+
5+
require (
6+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
7+
github.com/gin-gonic/gin v1.6.3
8+
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
9+
github.com/swaggo/gin-swagger v1.2.0
10+
github.com/swaggo/swag v1.5.1
11+
)

example/object-map-example/main.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/swaggo/swag/example/object-map-example/controller"
6+
_ "github.com/swaggo/swag/example/object-map-example/docs"
7+
8+
swaggerFiles "github.com/swaggo/files"
9+
ginSwagger "github.com/swaggo/gin-swagger"
10+
)
11+
12+
// @title Swagger Map Example API
13+
// @version 1.0
14+
// @termsOfService http://swagger.io/terms/
15+
16+
// @license.name Apache 2.0
17+
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
18+
19+
// @host localhost:8080
20+
// @BasePath /api/v1
21+
func main() {
22+
r := gin.Default()
23+
24+
c := controller.NewController()
25+
26+
v1 := r.Group("/api/v1")
27+
{
28+
test := v1.Group("/map")
29+
{
30+
test.GET("", c.GetMap)
31+
}
32+
}
33+
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
34+
r.Run(":8080")
35+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-openapi/spec v0.19.14
99
github.com/go-playground/validator/v10 v10.4.1 // indirect
1010
github.com/gofrs/uuid v3.3.0+incompatible
11+
github.com/shopspring/decimal v1.2.0
1112
github.com/golang/protobuf v1.4.3 // indirect
1213
github.com/json-iterator/go v1.1.10 // indirect
1314
github.com/mailru/easyjson v0.7.6 // indirect

0 commit comments

Comments
 (0)