Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-503: added type map in example value #683

Merged
merged 15 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions example/object-map-example/controller/api.go
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",
},
})
}
10 changes: 10 additions & 0 deletions example/object-map-example/controller/controller.go
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{}
}
11 changes: 11 additions & 0 deletions example/object-map-example/controller/response.go
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"`
}
141 changes: 141 additions & 0 deletions example/object-map-example/docs/docs.go
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{})
}
78 changes: 78 additions & 0 deletions example/object-map-example/docs/swagger.json
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": "Карта"
}
}
}
}
}
}
53 changes: 53 additions & 0 deletions example/object-map-example/docs/swagger.yaml
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"
11 changes: 11 additions & 0 deletions example/object-map-example/go.mod
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
)
35 changes: 35 additions & 0 deletions example/object-map-example/main.go
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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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
Loading