From 606f44e683b68d130403207e32f1614c00f113f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Sun, 25 Feb 2024 10:53:51 +0000 Subject: [PATCH] OpenAPI RESTful API code generator This patch includes and uses the oapi-codegen tool to auto-generate go boilerplate code, based on the OpenAPI spec. --- go.mod | 29 ++++ go.sum | 65 ++++++++ internal/api/impl.go | 26 ++++ internal/api/server.gen.go | 295 +++++++++++++++++++++++++++++++++++++ 4 files changed, 415 insertions(+) create mode 100644 go.sum create mode 100644 internal/api/impl.go create mode 100644 internal/api/server.gen.go diff --git a/go.mod b/go.mod index 59bf1d7..1b43872 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,32 @@ module git.rosemyrtle.work/personal-finance/server go 1.21.1 + +require ( + github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/deepmap/oapi-codegen/v2 v2.1.0 // indirect + github.com/getkin/kin-openapi v0.123.0 // indirect + github.com/go-openapi/jsonpointer v0.20.2 // indirect + github.com/go-openapi/swag v0.22.8 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/invopop/yaml v0.2.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/labstack/echo/v4 v4.11.4 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect + github.com/oapi-codegen/runtime v1.1.1 // indirect + github.com/perimeterx/marshmallow v1.1.5 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.17.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.12.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..93975bb --- /dev/null +++ b/go.sum @@ -0,0 +1,65 @@ +github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= +github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= +github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRdemvOZWyQ= +github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8= +github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8= +github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM= +github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= +github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= +github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw= +github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= +github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= +github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= +github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= +github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro= +github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= +github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= +github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= +golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/api/impl.go b/internal/api/impl.go new file mode 100644 index 0000000..8a6b90a --- /dev/null +++ b/internal/api/impl.go @@ -0,0 +1,26 @@ +package api + +//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=api.cfg.yaml ../../docs/openapi.yaml +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +type ServerImpl struct{} + +func (*ServerImpl) GetBanks(ctx echo.Context) error { + return echo.NewHTTPError(http.StatusNotImplemented) +} + +func (*ServerImpl) GetBanksById(ctx echo.Context, bankId int64) error { + return echo.NewHTTPError(http.StatusNotImplemented) +} + +func (*ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error { + return echo.NewHTTPError(http.StatusNotImplemented) +} + +func (*ServerImpl) GetTransactionsById(ctx echo.Context, transactionId int64) error { + return echo.NewHTTPError(http.StatusNotImplemented) +} diff --git a/internal/api/server.gen.go b/internal/api/server.gen.go new file mode 100644 index 0000000..2c07977 --- /dev/null +++ b/internal/api/server.gen.go @@ -0,0 +1,295 @@ +// Package api provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT. +package api + +import ( + "bytes" + "compress/gzip" + "encoding/base64" + "fmt" + "net/http" + "net/url" + "path" + "strings" + + "github.com/getkin/kin-openapi/openapi3" + "github.com/labstack/echo/v4" + "github.com/oapi-codegen/runtime" + openapi_types "github.com/oapi-codegen/runtime/types" +) + +// Bank defines model for Bank. +type Bank struct { + Id string `json:"id"` + Name string `json:"name"` + NordigenId *openapi_types.UUID `json:"nordigenId,omitempty"` +} + +// Banks defines model for Banks. +type Banks = []Bank + +// Transaction defines model for Transaction. +type Transaction struct { + Category *string `json:"category,omitempty"` + Date openapi_types.Date `json:"date"` + Description string `json:"description"` + Id int64 `json:"id"` + Value float32 `json:"value"` +} + +// Transactions defines model for Transactions. +type Transactions = []Transaction + +// GetTransactionsParams defines parameters for GetTransactions. +type GetTransactionsParams struct { + // Category filter by transaction category + Category *string `form:"category,omitempty" json:"category,omitempty"` + + // Limit number of transactions to return + Limit *int32 `form:"limit,omitempty" json:"limit,omitempty"` + + // Offset offset from where to retrieve transactions + Offset *int32 `form:"offset,omitempty" json:"offset,omitempty"` + + // Bank ID of the bank + Bank *string `form:"bank,omitempty" json:"bank,omitempty"` + + // Sort field name by which to sort + Sort *string `form:"sort,omitempty" json:"sort,omitempty"` +} + +// ServerInterface represents all server handlers. +type ServerInterface interface { + // Retrieve existing banks + // (GET /banks) + GetBanks(ctx echo.Context) error + // Find bank by ID + // (GET /banks/{bankId}) + GetBanksById(ctx echo.Context, bankId int64) error + // Retrieve existing transactions + // (GET /transactions) + GetTransactions(ctx echo.Context, params GetTransactionsParams) error + // Find transaction by ID + // (GET /transactions/{transactionId}) + GetTransactionsById(ctx echo.Context, transactionId int64) error +} + +// ServerInterfaceWrapper converts echo contexts to parameters. +type ServerInterfaceWrapper struct { + Handler ServerInterface +} + +// GetBanks converts echo context to params. +func (w *ServerInterfaceWrapper) GetBanks(ctx echo.Context) error { + var err error + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetBanks(ctx) + return err +} + +// GetBanksById converts echo context to params. +func (w *ServerInterfaceWrapper) GetBanksById(ctx echo.Context) error { + var err error + // ------------- Path parameter "bankId" ------------- + var bankId int64 + + err = runtime.BindStyledParameterWithOptions("simple", "bankId", ctx.Param("bankId"), &bankId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bankId: %s", err)) + } + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetBanksById(ctx, bankId) + return err +} + +// GetTransactions converts echo context to params. +func (w *ServerInterfaceWrapper) GetTransactions(ctx echo.Context) error { + var err error + + // Parameter object where we will unmarshal all parameters from the context + var params GetTransactionsParams + // ------------- Optional query parameter "category" ------------- + + err = runtime.BindQueryParameter("form", true, false, "category", ctx.QueryParams(), ¶ms.Category) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter category: %s", err)) + } + + // ------------- Optional query parameter "limit" ------------- + + err = runtime.BindQueryParameter("form", true, false, "limit", ctx.QueryParams(), ¶ms.Limit) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter limit: %s", err)) + } + + // ------------- Optional query parameter "offset" ------------- + + err = runtime.BindQueryParameter("form", true, false, "offset", ctx.QueryParams(), ¶ms.Offset) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter offset: %s", err)) + } + + // ------------- Optional query parameter "bank" ------------- + + err = runtime.BindQueryParameter("form", true, false, "bank", ctx.QueryParams(), ¶ms.Bank) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bank: %s", err)) + } + + // ------------- Optional query parameter "sort" ------------- + + err = runtime.BindQueryParameter("form", true, false, "sort", ctx.QueryParams(), ¶ms.Sort) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter sort: %s", err)) + } + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetTransactions(ctx, params) + return err +} + +// GetTransactionsById converts echo context to params. +func (w *ServerInterfaceWrapper) GetTransactionsById(ctx echo.Context) error { + var err error + // ------------- Path parameter "transactionId" ------------- + var transactionId int64 + + err = runtime.BindStyledParameterWithOptions("simple", "transactionId", ctx.Param("transactionId"), &transactionId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter transactionId: %s", err)) + } + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetTransactionsById(ctx, transactionId) + return err +} + +// This is a simple interface which specifies echo.Route addition functions which +// are present on both echo.Echo and echo.Group, since we want to allow using +// either of them for path registration +type EchoRouter interface { + CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route + TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route +} + +// RegisterHandlers adds each server route to the EchoRouter. +func RegisterHandlers(router EchoRouter, si ServerInterface) { + RegisterHandlersWithBaseURL(router, si, "") +} + +// Registers handlers, and prepends BaseURL to the paths, so that the paths +// can be served under a prefix. +func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) { + + wrapper := ServerInterfaceWrapper{ + Handler: si, + } + + router.GET(baseURL+"/banks", wrapper.GetBanks) + router.GET(baseURL+"/banks/:bankId", wrapper.GetBanksById) + router.GET(baseURL+"/transactions", wrapper.GetTransactions) + router.GET(baseURL+"/transactions/:transactionId", wrapper.GetTransactionsById) + +} + +// Base64 encoded, gzipped, json marshaled Swagger object +var swaggerSpec = []string{ + + "H4sIAAAAAAAC/8xWX2/bNhD/KgS3R0VS6mAPetqKoIGBoQvW7anrAy2dZHb8o5JHZ0bg7z4cKTuSJdcZ", + "sA57iSXzePf7c3fOM6+t7q0Bg55Xz9zXW9AiPr4V5k/67J3twaGE+K1s6C/ue+AV9+ik6fgh40ZoWD6w", + "rpEdmHW811qnBfKKhyAbnp2HHzLu4EuQDhpefeQxJGb+dAq1m89QI2UmfAkSgo4P3ztoecW/K144FQOh", + "IrI5nNII58Se3n9zwnhRo7RmTrYWCJ11+0VmjUCYcIpfZAuB4Gsn+2OJ2bmcSiMN/nDHM66lkTpoXpWn", + "nNIgdODo0k6oMC3fKivwpb4JekOhS5oOSMfAjhmXlB5p9HrBx8LOdCdU0rQ2imwNihrpEbSQiqhII0wN", + "P+rgUOQN7GYy8kdw3hqh2LsUyz6A24H7g5goWYPxUZ3Ul/zh/e/sAQw4odhj2ChZs59TENut8pJZx5RA", + "cDzjwRGCLWLvq6J4enrKOxNy67piSOuLrlc3q7y88ShMI5Q1kG9Rq0hTooIleDfslx7MT49rtspLUhuc", + "T0zKvMxv6a7twYhe8oqv8jJf8Yz3ArdR6GJzbPYOolLUpYKkoLniD4BpGshr31tCSVFvyvKoMJh4T/S9", + "knW8WXz2qSGTY68ZIJ+cmzrxIdQ1eN8GxU6oiM5deUc5p8HvLdsMiTLug9aCpov/Cugk7IDBX9KjNN0o", + "KnEvnulj3RyuivB2v26idk5oQHCeVx/PYazvmW1jDYaWOcDgqHEknZHox71T8VSVj4cIXYBspNpscs+n", + "9fDpG/vyz2wp57aszU4o2bD1PfOBoEBz0UIqyIxF1tpgmjMj30nTJF03e7a+Twbi2f64ZN9kz1xxsJUK", + "wVGVUXZ2WtiDl18CxJfBzNHxi7gmKCU2NLXJ2Nlv0nnptFmpgcbE5o10VlxJLXFSuYFWBIW8ui3LbNJG", + "qzdXfgDmqGzbekDWOqvZ0xYcDIDSXOFU2iV8KcEywJvbBXzXMaUxwy3ElrhQdzh6qXrVgFaCahhdJ/+f", + "trLeEllvHV6oMRxdrvEtJ3TS1v/CAsVpvq/v0VnwZBqL59HbleU6pvH6HTueziurdgLlf7xxJ//Z/IeL", + "d1T36/t3rPlxDR8OfwcAAP//XTQsSusLAAA=", +} + +// GetSwagger returns the content of the embedded swagger specification file +// or error if failed to decode +func decodeSpec() ([]byte, error) { + zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, "")) + if err != nil { + return nil, fmt.Errorf("error base64 decoding spec: %w", err) + } + zr, err := gzip.NewReader(bytes.NewReader(zipped)) + if err != nil { + return nil, fmt.Errorf("error decompressing spec: %w", err) + } + var buf bytes.Buffer + _, err = buf.ReadFrom(zr) + if err != nil { + return nil, fmt.Errorf("error decompressing spec: %w", err) + } + + return buf.Bytes(), nil +} + +var rawSpec = decodeSpecCached() + +// a naive cached of a decoded swagger spec +func decodeSpecCached() func() ([]byte, error) { + data, err := decodeSpec() + return func() ([]byte, error) { + return data, err + } +} + +// Constructs a synthetic filesystem for resolving external references when loading openapi specifications. +func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) { + res := make(map[string]func() ([]byte, error)) + if len(pathToFile) > 0 { + res[pathToFile] = rawSpec + } + + return res +} + +// GetSwagger returns the Swagger specification corresponding to the generated code +// in this file. The external references of Swagger specification are resolved. +// The logic of resolving external references is tightly connected to "import-mapping" feature. +// Externally referenced files must be embedded in the corresponding golang packages. +// Urls can be supported but this task was out of the scope. +func GetSwagger() (swagger *openapi3.T, err error) { + resolvePath := PathToRawSpec("") + + loader := openapi3.NewLoader() + loader.IsExternalRefsAllowed = true + loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) { + pathToFile := url.String() + pathToFile = path.Clean(pathToFile) + getSpec, ok := resolvePath[pathToFile] + if !ok { + err1 := fmt.Errorf("path not found: %s", pathToFile) + return nil, err1 + } + return getSpec() + } + var specData []byte + specData, err = rawSpec() + if err != nil { + return + } + swagger, err = loader.LoadFromData(specData) + if err != nil { + return + } + return +}