-
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-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 <easonlin404@gmail.com>
- Loading branch information
1 parent
09f9621
commit c9056f0
Showing
15 changed files
with
547 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package controller | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
// GetMap godoc | ||
// @Summary Get Map Example | ||
// @Description get map | ||
// @ID get-map | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {object} Response | ||
// @Router /test [get] | ||
func (c *Controller) GetMap(ctx *gin.Context) { | ||
ctx.JSON(200, Response{ | ||
Title: map[string]string{ | ||
"en": "Map", | ||
}, | ||
CustomType: map[string]interface{}{ | ||
"key": "value", | ||
}, | ||
Object: Data{ | ||
Text: "object text", | ||
}, | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package controller | ||
|
||
// Controller example | ||
type Controller struct { | ||
} | ||
|
||
// NewController example | ||
func NewController() *Controller { | ||
return &Controller{} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package controller | ||
|
||
type Response struct { | ||
Title map[string]string `json:"title" example:"en:Map,ru:Карта,kk:Карталар"` | ||
CustomType map[string]interface{} `json:"map_data" swaggertype:"object,string" example:"key:value,key2:value2"` | ||
Object Data `json:"object"` | ||
} | ||
|
||
type Data struct { | ||
Text string `json:"title" example:"Object data"` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT | ||
// This file was generated by swaggo/swag | ||
|
||
package docs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/alecthomas/template" | ||
"github.com/swaggo/swag" | ||
) | ||
|
||
var doc = `{ | ||
"schemes": {{ marshal .Schemes }}, | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "{{.Description}}", | ||
"title": "{{.Title}}", | ||
"termsOfService": "http://swagger.io/terms/", | ||
"contact": {}, | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" | ||
}, | ||
"version": "{{.Version}}" | ||
}, | ||
"host": "{{.Host}}", | ||
"basePath": "{{.BasePath}}", | ||
"paths": { | ||
"/test": { | ||
"get": { | ||
"description": "get map", | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Get Map Example", | ||
"operationId": "get-map", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/controller.Response" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"controller.Data": { | ||
"type": "object", | ||
"properties": { | ||
"title": { | ||
"type": "string", | ||
"example": "Object data" | ||
} | ||
} | ||
}, | ||
"controller.Response": { | ||
"type": "object", | ||
"properties": { | ||
"map_data": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "string" | ||
}, | ||
"example": { | ||
"key": "value", | ||
"key2": "value2" | ||
} | ||
}, | ||
"object": { | ||
"$ref": "#/definitions/controller.Data" | ||
}, | ||
"title": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "string" | ||
}, | ||
"example": { | ||
"en": "Map", | ||
"kk": "Карталар", | ||
"ru": "Карта" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}` | ||
|
||
type swaggerInfo struct { | ||
Version string | ||
Host string | ||
BasePath string | ||
Schemes []string | ||
Title string | ||
Description string | ||
} | ||
|
||
// SwaggerInfo holds exported Swagger Info so clients can modify it | ||
var SwaggerInfo = swaggerInfo{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/api/v1", | ||
Schemes: []string{}, | ||
Title: "Swagger Map Example API", | ||
Description: "", | ||
} | ||
|
||
type s struct{} | ||
|
||
func (s *s) ReadDoc() string { | ||
sInfo := SwaggerInfo | ||
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1) | ||
|
||
t, err := template.New("swagger_info").Funcs(template.FuncMap{ | ||
"marshal": func(v interface{}) string { | ||
a, _ := json.Marshal(v) | ||
return string(a) | ||
}, | ||
}).Parse(doc) | ||
if err != nil { | ||
return doc | ||
} | ||
|
||
var tpl bytes.Buffer | ||
if err := t.Execute(&tpl, sInfo); err != nil { | ||
return doc | ||
} | ||
|
||
return tpl.String() | ||
} | ||
|
||
func init() { | ||
swag.Register(swag.Name, &s{}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "Swagger Map Example API", | ||
"termsOfService": "http://swagger.io/terms/", | ||
"contact": {}, | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" | ||
}, | ||
"version": "1.0" | ||
}, | ||
"host": "localhost:8080", | ||
"basePath": "/api/v1", | ||
"paths": { | ||
"/test": { | ||
"get": { | ||
"description": "get map", | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Get Map Example", | ||
"operationId": "get-map", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/controller.Response" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"controller.Data": { | ||
"type": "object", | ||
"properties": { | ||
"title": { | ||
"type": "string", | ||
"example": "Object data" | ||
} | ||
} | ||
}, | ||
"controller.Response": { | ||
"type": "object", | ||
"properties": { | ||
"map_data": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "string" | ||
}, | ||
"example": { | ||
"key": "value", | ||
"key2": "value2" | ||
} | ||
}, | ||
"object": { | ||
"$ref": "#/definitions/controller.Data" | ||
}, | ||
"title": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "string" | ||
}, | ||
"example": { | ||
"en": "Map", | ||
"kk": "Карталар", | ||
"ru": "Карта" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
basePath: /api/v1 | ||
definitions: | ||
controller.Data: | ||
properties: | ||
title: | ||
example: Object data | ||
type: string | ||
type: object | ||
controller.Response: | ||
properties: | ||
map_data: | ||
additionalProperties: | ||
type: string | ||
example: | ||
key: value | ||
key2: value2 | ||
type: object | ||
object: | ||
$ref: '#/definitions/controller.Data' | ||
title: | ||
additionalProperties: | ||
type: string | ||
example: | ||
en: Map | ||
kk: Карталар | ||
ru: Карта | ||
type: object | ||
type: object | ||
host: localhost:8080 | ||
info: | ||
contact: {} | ||
license: | ||
name: Apache 2.0 | ||
url: http://www.apache.org/licenses/LICENSE-2.0.html | ||
termsOfService: http://swagger.io/terms/ | ||
title: Swagger Map Example API | ||
version: "1.0" | ||
paths: | ||
/test: | ||
get: | ||
consumes: | ||
- application/json | ||
description: get map | ||
operationId: get-map | ||
produces: | ||
- application/json | ||
responses: | ||
"200": | ||
description: OK | ||
schema: | ||
$ref: '#/definitions/controller.Response' | ||
summary: Get Map Example | ||
swagger: "2.0" |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/swaggo/swag/example/object-map-example | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 | ||
github.com/gin-gonic/gin v1.6.3 | ||
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 | ||
github.com/swaggo/gin-swagger v1.2.0 | ||
github.com/swaggo/swag v1.5.1 | ||
) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/swaggo/swag/example/object-map-example/controller" | ||
_ "github.com/swaggo/swag/example/object-map-example/docs" | ||
|
||
swaggerFiles "github.com/swaggo/files" | ||
ginSwagger "github.com/swaggo/gin-swagger" | ||
) | ||
|
||
// @title Swagger Map Example API | ||
// @version 1.0 | ||
// @termsOfService http://swagger.io/terms/ | ||
|
||
// @license.name Apache 2.0 | ||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
// @host localhost:8080 | ||
// @BasePath /api/v1 | ||
func main() { | ||
r := gin.Default() | ||
|
||
c := controller.NewController() | ||
|
||
v1 := r.Group("/api/v1") | ||
{ | ||
test := v1.Group("/map") | ||
{ | ||
test.GET("", c.GetMap) | ||
} | ||
} | ||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) | ||
r.Run(":8080") | ||
} |
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
Oops, something went wrong.