Skip to content

Commit b1a296e

Browse files
committed
FEATURE/MEDIUM: specification: add endpoint to serve OpenAPI v3 spec
This adds endpoint to serve OpenAPI v3 specification in addition to existing endpoint (serving Swagger v2)
1 parent 99e6860 commit b1a296e

10 files changed

+524
-1
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Data Plane API is generated using [go-swagger](https://github.com/go-swagger/go-
1919
--tags=Discovery \
2020
--tags=Information \
2121
--tags=Specification \
22+
--tags=SpecificationOpenapiv3 \
2223
--tags=Transactions \
2324
--tags=Sites \
2425
--tags=Stats \

configure_data_plane.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ import (
3434
"syscall"
3535
"time"
3636

37+
"github.com/getkin/kin-openapi/openapi2"
38+
"github.com/getkin/kin-openapi/openapi2conv"
3739
"github.com/haproxytech/config-parser/v2/types"
3840
"github.com/haproxytech/dataplaneapi/adapters"
3941
"github.com/haproxytech/dataplaneapi/operations/specification"
42+
"github.com/haproxytech/dataplaneapi/operations/specification_openapiv3"
4043
"github.com/haproxytech/models/v2"
4144

4245
log "github.com/sirupsen/logrus"
@@ -433,6 +436,30 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
433436
return specification.NewGetSpecificationOK().WithPayload(&m)
434437
})
435438

439+
// setup OpenAPI v3 specification handler
440+
api.SpecificationOpenapiv3GetOpenapiv3SpecificationHandler = specification_openapiv3.GetOpenapiv3SpecificationHandlerFunc(func(params specification_openapiv3.GetOpenapiv3SpecificationParams, principal interface{}) middleware.Responder {
441+
v2 := openapi2.Swagger{}
442+
err := v2.UnmarshalJSON(SwaggerJSON)
443+
if err != nil {
444+
e := misc.HandleError(err)
445+
return specification_openapiv3.NewGetOpenapiv3SpecificationDefault(int(*e.Code)).WithPayload(e)
446+
}
447+
448+
// if host is empty(dynamic hosts), server prop is empty,
449+
// so we need to set it explicitly
450+
if v2.Host == "" {
451+
cfg := dataplaneapi_config.Get()
452+
v2.Host = cfg.Server.Host
453+
}
454+
455+
v3, err := openapi2conv.ToV3Swagger(&v2)
456+
if err != nil {
457+
e := misc.HandleError(err)
458+
return specification_openapiv3.NewGetOpenapiv3SpecificationDefault(int(*e.Code)).WithPayload(e)
459+
}
460+
return specification_openapiv3.NewGetOpenapiv3SpecificationOK().WithPayload(v3)
461+
})
462+
436463
return setupGlobalMiddleware(api.Serve(setupMiddlewares))
437464
}
438465

embedded_spec.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
88
github.com/docker/go-units v0.4.0
99
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0
10+
github.com/getkin/kin-openapi v0.17.0
1011
github.com/go-ole/go-ole v1.2.4 // indirect
1112
github.com/go-openapi/errors v0.19.4
1213
github.com/go-openapi/loads v0.19.5
@@ -25,5 +26,5 @@ require (
2526
github.com/sirupsen/logrus v1.5.0
2627
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0
2728
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f
28-
gopkg.in/yaml.v2 v2.2.8
29+
gopkg.in/yaml.v2 v2.3.0
2930
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
2323
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
2424
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0 h1:90Ly+6UfUypEF6vvvW5rQIv9opIL8CbmW9FT20LDQoY=
2525
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0/go.mod h1:V+Qd57rJe8gd4eiGzZyg4h54VLHmYVVw54iMnlAMrF8=
26+
github.com/getkin/kin-openapi v0.17.0 h1:eAwanndaPI4Hw/vEF3Twj54j2gUzpbSf7BD3lEl6ARg=
27+
github.com/getkin/kin-openapi v0.17.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
28+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
29+
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
2630
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
2731
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
2832
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
@@ -282,3 +286,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
282286
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
283287
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
284288
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
289+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
290+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

operations/data_plane_api.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operations/specification_openapiv3/get_openapiv3_specification.go

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operations/specification_openapiv3/get_openapiv3_specification_parameters.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)