Skip to content

Commit

Permalink
issue-503: added type map in example value (#683)
Browse files Browse the repository at this point in the history
* 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
Arrim and easonlin404 committed Nov 20, 2020
1 parent 09f9621 commit c9056f0
Show file tree
Hide file tree
Showing 15 changed files with 547 additions and 15 deletions.
25 changes: 25 additions & 0 deletions example/object-map-example/controller/api.go
@@ -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",
},
})
}
10 changes: 10 additions & 0 deletions example/object-map-example/controller/controller.go
@@ -0,0 +1,10 @@
package controller

// Controller example
type Controller struct {
}

// NewController example
func NewController() *Controller {
return &Controller{}
}
11 changes: 11 additions & 0 deletions example/object-map-example/controller/response.go
@@ -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"`
}
141 changes: 141 additions & 0 deletions example/object-map-example/docs/docs.go
@@ -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{})
}
78 changes: 78 additions & 0 deletions example/object-map-example/docs/swagger.json
@@ -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": "Карта"
}
}
}
}
}
}
53 changes: 53 additions & 0 deletions example/object-map-example/docs/swagger.yaml
@@ -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"
11 changes: 11 additions & 0 deletions example/object-map-example/go.mod
@@ -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
)
35 changes: 35 additions & 0 deletions example/object-map-example/main.go
@@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-openapi/spec v0.19.14
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gofrs/uuid v3.3.0+incompatible
github.com/shopspring/decimal v1.2.0
github.com/golang/protobuf v1.4.3 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
Expand Down

0 comments on commit c9056f0

Please sign in to comment.