Skip to content

Commit 3375f28

Browse files
committed
Added on top of #3127:
* resolved merge conflicts * enabled the new feature on option only (new flag available), since this breaks the expected behavior of a jsonschema draft4 document ($ref with siblings appear in jsonschema only much later) * documented the new CLI flag for "swagger generate spec" to get the expected tags alongside the $ref * added to the tests the complete repro example provided by the op in #3125 * added the fix and test for #2540 (which is unrelated to #3124) * added --format yaml|json option to generate spec explicitly as json or yaml, even on stdout Fixes: * #3125 * #3094 (duplicate) * #2540 (unrelated to #1325) Unfortunately, these fixes are unrelated to #2106, which we cannot claim as fixed yet. Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent af80ca3 commit 3375f28

10 files changed

Lines changed: 387 additions & 1 deletion

File tree

fixtures/bugs/2540/foo/examples.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Package docs SwagTest
2+
//
3+
// Test Swagger
4+
//
5+
// Schemes: https
6+
// Version: 1.0.0
7+
// BasePath: /test/
8+
//
9+
// Consumes:
10+
// - application/json
11+
//
12+
// Produces:
13+
// - application/json
14+
//
15+
// swagger:meta
16+
package foo
17+
18+
// swagger:route GET /book book
19+
// Get book
20+
// responses:
21+
// 200: getBook
22+
23+
type Author struct {
24+
Name string
25+
}
26+
27+
// Book holds all relevant information about a book.
28+
//
29+
// At this moment, a book is only described by its publishing date
30+
// and author.
31+
//
32+
// example: { "Published": 2026, "Author": "Fred" }
33+
//
34+
// default: { "Published": 1900, "Author": "Unknown" }
35+
//
36+
// swagger:model
37+
type Book struct {
38+
// min: 0
39+
//
40+
// example: 2021
41+
Published int
42+
// example: { "Name": "Tolkien" }
43+
Author Author
44+
}
45+
46+
// OK.
47+
// swagger:response getBook
48+
type response struct {
49+
// in:body
50+
Body Book
51+
}

fixtures/bugs/3125/full/api/api.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo/v4"
7+
)
8+
9+
type User struct {
10+
Name string `json:"name"`
11+
Age int `json:"age"`
12+
}
13+
14+
// FooBarRequest represents body of FooBar request.
15+
type FooBarRequest struct {
16+
// Foo param
17+
Foo string `json:"foo"`
18+
// Bar params
19+
Bar []int `json:"bar"`
20+
// User param
21+
User User `json:"user"`
22+
}
23+
24+
// FooBarResponse represents body of FooBar response.
25+
type FooBarResponse struct {
26+
Baz struct {
27+
Prop string `json:"prop"`
28+
} `json:"baz"`
29+
}
30+
31+
// FooBarHandler handles incoming foobar requests
32+
func FooBarHandler(ctx echo.Context) error {
33+
req := FooBarRequest{}
34+
if err := ctx.Bind(&req); err != nil {
35+
return echo.ErrBadRequest
36+
}
37+
resp := doSthWithRequest(req)
38+
return ctx.JSON(http.StatusOK, resp)
39+
}
40+
41+
func doSthWithRequest(req FooBarRequest) FooBarResponse {
42+
return FooBarResponse{}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Package classification awesome.
2+
//
3+
// Documentation of our awesome API.
4+
//
5+
// Schemes: http
6+
// BasePath: /
7+
// Version: 1.0.0
8+
// Host: some-url.com
9+
//
10+
// Consumes:
11+
// - application/json
12+
//
13+
// Produces:
14+
// - application/json
15+
//
16+
// Security:
17+
// - basic
18+
//
19+
// SecurityDefinitions:
20+
// basic:
21+
// type: basic
22+
//
23+
// swagger:meta
24+
package docs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package docs
2+
3+
import "swagger/api"
4+
5+
// swagger:route POST /foobar foobar-tag idOfFoobarEndpoint
6+
// Foobar does some amazing stuff.
7+
// responses:
8+
// 200: foobarResponse
9+
10+
// This text will appear as description of your response body.
11+
// swagger:response foobarResponse
12+
type foobarResponseWrapper struct {
13+
// in:body
14+
Body api.FooBarResponse
15+
}
16+
17+
// swagger:parameters idOfFoobarEndpoint
18+
type foobarParamsWrapper struct {
19+
// This text will appear as description of your request body.
20+
// in:body
21+
Body api.FooBarRequest
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
basePath: /
2+
consumes:
3+
- application/json
4+
definitions:
5+
FooBarRequest:
6+
properties:
7+
bar:
8+
description: Bar params
9+
items:
10+
format: int64
11+
type: integer
12+
type: array
13+
x-go-name: Bar
14+
foo:
15+
description: Foo param
16+
type: string
17+
x-go-name: Foo
18+
user:
19+
description: User param
20+
$ref: '#/definitions/User'
21+
title: FooBarRequest represents body of FooBar request.
22+
type: object
23+
x-go-package: swagger/api
24+
FooBarResponse:
25+
properties:
26+
baz:
27+
properties:
28+
prop:
29+
type: string
30+
x-go-name: Prop
31+
type: object
32+
x-go-name: Baz
33+
title: FooBarResponse represents body of FooBar response.
34+
type: object
35+
x-go-package: swagger/api
36+
User:
37+
properties:
38+
age:
39+
format: int64
40+
type: integer
41+
x-go-name: Age
42+
name:
43+
type: string
44+
x-go-name: Name
45+
type: object
46+
x-go-package: swagger/api
47+
host: some-url.com
48+
info:
49+
description: Documentation of our awesome API.
50+
title: awesome.
51+
version: 1.0.0
52+
paths:
53+
/foobar:
54+
post:
55+
operationId: idOfFoobarEndpoint
56+
parameters:
57+
- description: This text will appear as description of your request body.
58+
in: body
59+
name: Body
60+
schema:
61+
$ref: '#/definitions/FooBarRequest'
62+
responses:
63+
"200":
64+
$ref: '#/responses/foobarResponse'
65+
summary: Foobar does some amazing stuff.
66+
tags:
67+
- foobar-tag
68+
produces:
69+
- application/json
70+
responses:
71+
foobarResponse:
72+
description: This text will appear as description of your response body.
73+
schema:
74+
$ref: '#/definitions/FooBarResponse'
75+
schemes:
76+
- http
77+
securityDefinitions:
78+
basic:
79+
type: basic
80+
swagger: "2.0"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
basePath: /
2+
consumes:
3+
- application/json
4+
definitions:
5+
FooBarRequest:
6+
properties:
7+
bar:
8+
description: Bar params
9+
items:
10+
format: int64
11+
type: integer
12+
type: array
13+
x-go-name: Bar
14+
foo:
15+
description: Foo param
16+
type: string
17+
x-go-name: Foo
18+
user:
19+
$ref: '#/definitions/User'
20+
title: FooBarRequest represents body of FooBar request.
21+
type: object
22+
x-go-package: swagger/api
23+
FooBarResponse:
24+
properties:
25+
baz:
26+
properties:
27+
prop:
28+
type: string
29+
x-go-name: Prop
30+
type: object
31+
x-go-name: Baz
32+
title: FooBarResponse represents body of FooBar response.
33+
type: object
34+
x-go-package: swagger/api
35+
User:
36+
properties:
37+
age:
38+
format: int64
39+
type: integer
40+
x-go-name: Age
41+
name:
42+
type: string
43+
x-go-name: Name
44+
type: object
45+
x-go-package: swagger/api
46+
host: some-url.com
47+
info:
48+
description: Documentation of our awesome API.
49+
title: awesome.
50+
version: 1.0.0
51+
paths:
52+
/foobar:
53+
post:
54+
operationId: idOfFoobarEndpoint
55+
parameters:
56+
- description: This text will appear as description of your request body.
57+
in: body
58+
name: Body
59+
schema:
60+
$ref: '#/definitions/FooBarRequest'
61+
responses:
62+
"200":
63+
$ref: '#/responses/foobarResponse'
64+
summary: Foobar does some amazing stuff.
65+
tags:
66+
- foobar-tag
67+
produces:
68+
- application/json
69+
responses:
70+
foobarResponse:
71+
description: This text will appear as description of your response body.
72+
schema:
73+
$ref: '#/definitions/FooBarResponse'
74+
schemes:
75+
- http
76+
securityDefinitions:
77+
basic:
78+
type: basic
79+
swagger: "2.0"

fixtures/bugs/3125/full/go.mod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module swagger
2+
3+
go 1.24.0
4+
5+
require github.com/labstack/echo/v4 v4.12.0
6+
7+
require (
8+
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
9+
github.com/labstack/gommon v0.4.2 // indirect
10+
github.com/mattn/go-colorable v0.1.13 // indirect
11+
github.com/mattn/go-isatty v0.0.20 // indirect
12+
github.com/stretchr/testify v1.9.0 // indirect
13+
github.com/valyala/bytebufferpool v1.0.0 // indirect
14+
github.com/valyala/fasttemplate v1.2.2 // indirect
15+
golang.org/x/crypto v0.25.0 // indirect
16+
golang.org/x/net v0.24.0 // indirect
17+
golang.org/x/sys v0.22.0 // indirect
18+
golang.org/x/text v0.16.0 // indirect
19+
golang.org/x/time v0.5.0 // indirect
20+
)

fixtures/bugs/3125/full/go.sum

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
4+
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
5+
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
6+
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
7+
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
8+
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
9+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
10+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
11+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
12+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
13+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
14+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
17+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
18+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
19+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
20+
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
21+
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
22+
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
23+
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
24+
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
25+
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
26+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
29+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
30+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
31+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
32+
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
33+
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
34+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
35+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

fixtures/bugs/3125/full/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"swagger/api"
5+
6+
"github.com/labstack/echo/v4"
7+
"github.com/labstack/echo/v4/middleware"
8+
)
9+
10+
func main() {
11+
// Echo instance
12+
e := echo.New()
13+
14+
e.Use(middleware.Logger())
15+
e.Use(middleware.Recover())
16+
17+
// Set up basic auth with username=foo and password=bar
18+
e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
19+
Validator: func(username, password string, c echo.Context) (bool, error) {
20+
if username == "foo" && password == "bar" {
21+
return true, nil
22+
}
23+
return false, nil
24+
},
25+
}))
26+
27+
// Route => handler
28+
e.POST("/foobar", api.FooBarHandler)
29+
30+
// Start server
31+
e.Logger.Fatal(e.Start(":1323"))
32+
}

0 commit comments

Comments
 (0)