diff --git a/internal/test/issues/issue-1397/config.yaml b/internal/test/issues/issue-1397/config.yaml new file mode 100644 index 0000000000..d4a1284eeb --- /dev/null +++ b/internal/test/issues/issue-1397/config.yaml @@ -0,0 +1,8 @@ +# yaml-language-server: $schema=../../../../configuration-schema.json +package: issue1397 +generate: + echo-server: true + client: true + models: true + embedded-spec: true +output: issue1397.gen.go diff --git a/internal/test/issues/issue-1397/doc.go b/internal/test/issues/issue-1397/doc.go new file mode 100644 index 0000000000..4dd8581a3c --- /dev/null +++ b/internal/test/issues/issue-1397/doc.go @@ -0,0 +1,3 @@ +package issue1397 + +//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/issues/issue-1397/issue1397.gen.go b/internal/test/issues/issue-1397/issue1397.gen.go new file mode 100644 index 0000000000..36c9a79266 --- /dev/null +++ b/internal/test/issues/issue-1397/issue1397.gen.go @@ -0,0 +1,440 @@ +// Package issue1397 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.0.0-00010101000000-000000000000 DO NOT EDIT. +package issue1397 + +import ( + "bytes" + "compress/gzip" + "context" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "path" + "strings" + + "github.com/getkin/kin-openapi/openapi3" + "github.com/labstack/echo/v4" +) + +// Defines values for TestField1. +const ( + Option1 TestField1 = "option1" + Option2 TestField1 = "option2" +) + +// Test defines model for Test. +type Test = MyTestRequest + +// TestField1 defines model for Test.Field1. +type TestField1 string + +// MyTestRequestNestedField A nested object with allocated name +type MyTestRequestNestedField struct { + Field1 bool `json:"field1"` + Field2 string `json:"field2"` +} + +// MyTestRequest defines model for . +type MyTestRequest struct { + // Field1 A array of enum values + Field1 *[]TestField1 `json:"field1,omitempty"` + + // Field2 A nested object with allocated name + Field2 *MyTestRequestNestedField `json:"field2,omitempty"` + + // Field3 A nested object without allocated name + Field3 *struct { + Field1 bool `json:"field1"` + Field2 string `json:"field2"` + } `json:"field3,omitempty"` +} + +// TestApplicationTestPlusJSONRequestBody defines body for Test for application/test+json ContentType. +type TestApplicationTestPlusJSONRequestBody = Test + +// RequestEditorFn is the function signature for the RequestEditor callback function +type RequestEditorFn func(ctx context.Context, req *http.Request) error + +// Doer performs HTTP requests. +// +// The standard http.Client implements this interface. +type HttpRequestDoer interface { + Do(req *http.Request) (*http.Response, error) +} + +// Client which conforms to the OpenAPI3 specification for this service. +type Client struct { + // The endpoint of the server conforming to this interface, with scheme, + // https://api.deepmap.com for example. This can contain a path relative + // to the server, such as https://api.deepmap.com/dev-test, and all the + // paths in the swagger spec will be appended to the server. + Server string + + // Doer for performing requests, typically a *http.Client with any + // customized settings, such as certificate chains. + Client HttpRequestDoer + + // A list of callbacks for modifying requests which are generated before sending over + // the network. + RequestEditors []RequestEditorFn +} + +// ClientOption allows setting custom parameters during construction +type ClientOption func(*Client) error + +// Creates a new Client, with reasonable defaults +func NewClient(server string, opts ...ClientOption) (*Client, error) { + // create a client with sane default values + client := Client{ + Server: server, + } + // mutate client and add all optional params + for _, o := range opts { + if err := o(&client); err != nil { + return nil, err + } + } + // ensure the server URL always has a trailing slash + if !strings.HasSuffix(client.Server, "/") { + client.Server += "/" + } + // create httpClient, if not already present + if client.Client == nil { + client.Client = &http.Client{} + } + return &client, nil +} + +// WithHTTPClient allows overriding the default Doer, which is +// automatically created using http.Client. This is useful for tests. +func WithHTTPClient(doer HttpRequestDoer) ClientOption { + return func(c *Client) error { + c.Client = doer + return nil + } +} + +// WithRequestEditorFn allows setting up a callback function, which will be +// called right before sending the request. This can be used to mutate the request. +func WithRequestEditorFn(fn RequestEditorFn) ClientOption { + return func(c *Client) error { + c.RequestEditors = append(c.RequestEditors, fn) + return nil + } +} + +// The interface specification for the client above. +type ClientInterface interface { + // TestWithBody request with any body + TestWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + + TestWithApplicationTestPlusJSONBody(ctx context.Context, body TestApplicationTestPlusJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) +} + +func (c *Client) TestWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewTestRequestWithBody(c.Server, contentType, body) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +func (c *Client) TestWithApplicationTestPlusJSONBody(ctx context.Context, body TestApplicationTestPlusJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewTestRequestWithApplicationTestPlusJSONBody(c.Server, body) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +// NewTestRequestWithApplicationTestPlusJSONBody calls the generic Test builder with application/test+json body +func NewTestRequestWithApplicationTestPlusJSONBody(server string, body TestApplicationTestPlusJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewTestRequestWithBody(server, "application/test+json", bodyReader) +} + +// NewTestRequestWithBody generates requests for Test with any type of body +func NewTestRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/test") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), body) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) + + return req, nil +} + +func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error { + for _, r := range c.RequestEditors { + if err := r(ctx, req); err != nil { + return err + } + } + for _, r := range additionalEditors { + if err := r(ctx, req); err != nil { + return err + } + } + return nil +} + +// ClientWithResponses builds on ClientInterface to offer response payloads +type ClientWithResponses struct { + ClientInterface +} + +// NewClientWithResponses creates a new ClientWithResponses, which wraps +// Client with return type handling +func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) { + client, err := NewClient(server, opts...) + if err != nil { + return nil, err + } + return &ClientWithResponses{client}, nil +} + +// WithBaseURL overrides the baseURL. +func WithBaseURL(baseURL string) ClientOption { + return func(c *Client) error { + newBaseURL, err := url.Parse(baseURL) + if err != nil { + return err + } + c.Server = newBaseURL.String() + return nil + } +} + +// ClientWithResponsesInterface is the interface specification for the client with responses above. +type ClientWithResponsesInterface interface { + // TestWithBodyWithResponse request with any body + TestWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*TestResponse, error) + + TestWithApplicationTestPlusJSONBodyWithResponse(ctx context.Context, body TestApplicationTestPlusJSONRequestBody, reqEditors ...RequestEditorFn) (*TestResponse, error) +} + +type TestResponse struct { + Body []byte + HTTPResponse *http.Response +} + +// Status returns HTTPResponse.Status +func (r TestResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r TestResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +// TestWithBodyWithResponse request with arbitrary body returning *TestResponse +func (c *ClientWithResponses) TestWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*TestResponse, error) { + rsp, err := c.TestWithBody(ctx, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseTestResponse(rsp) +} + +func (c *ClientWithResponses) TestWithApplicationTestPlusJSONBodyWithResponse(ctx context.Context, body TestApplicationTestPlusJSONRequestBody, reqEditors ...RequestEditorFn) (*TestResponse, error) { + rsp, err := c.TestWithApplicationTestPlusJSONBody(ctx, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseTestResponse(rsp) +} + +// ParseTestResponse parses an HTTP response from a TestWithResponse call +func ParseTestResponse(rsp *http.Response) (*TestResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &TestResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + return response, nil +} + +// ServerInterface represents all server handlers. +type ServerInterface interface { + + // (GET /test) + Test(ctx echo.Context) error +} + +// ServerInterfaceWrapper converts echo contexts to parameters. +type ServerInterfaceWrapper struct { + Handler ServerInterface +} + +// Test converts echo context to params. +func (w *ServerInterfaceWrapper) Test(ctx echo.Context) error { + var err error + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.Test(ctx) + 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+"/test", wrapper.Test) + +} + +// Base64 encoded, gzipped, json marshaled Swagger object +var swaggerSpec = []string{ + + "H4sIAAAAAAAC/8xTO2/bMBD+K8K1W2XLj3bh1g4FPLRD4C3IQEsnmQbFo8lTYsHQfw+OUmzkBSRbJh2O", + "9/F76HiGklpPDh1HUGeI5R5bncotRpavD+QxsMHUrQ3aailVhbEMxrMhBwp+ZzoE3WdUZ+i6NrvXtsMI", + "ORjGNiGlDeoWKEGWkE/VCu5y4N4jKIgcjGtgyKHVp82I/HU5TQxymESs3hLhMDJWGe0OWHL2YHifaWup", + "1NJ1ukXI3zU0seyILGr3nOeFviGHgMfOBKzE0nTHBXA1NAqBHE6zhmbSnCUVCv71EvANHjuM/D/J/ivo", + "C+/6Y/6o4y9hUQCfMw2DYIyrCZTrrJWFQKe9AQXr+WIubF7zPnkoeFrHBtNH/GmJZVOBGnd1FIyR/1DV", + "y0xJjtGlce29NWUCpJt+HKIk+rTwUn0PWIOCb8X1RRTTcyi2k9wUSvTk4pjsavHz9V9qiKo0PAyPAQAA", + "///99gukXwMAAA==", +} + +// 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 +} diff --git a/internal/test/issues/issue-1397/spec.yaml b/internal/test/issues/issue-1397/spec.yaml new file mode 100644 index 0000000000..93cc609edf --- /dev/null +++ b/internal/test/issues/issue-1397/spec.yaml @@ -0,0 +1,52 @@ +openapi: "3.0.1" +components: + schemas: + Test: + type: object + properties: + field1: + description: A array of enum values + items: + enum: + - option1 + - option2 + type: string + maxItems: 5 + minItems: 0 + type: array + field2: + description: A nested object with allocated name + properties: + field1: + type: boolean + field2: + type: string + required: + - field1 + - field2 + type: object + x-go-type-name: MyTestRequestNestedField + field3: + description: A nested object without allocated name + properties: + field1: + type: boolean + field2: + type: string + required: + - field1 + - field2 + type: object + x-go-type-name: MyTestRequest +paths: + /test: + get: + operationId: test + requestBody: + content: + application/test+json: + schema: + $ref: "#/components/schemas/Test" + responses: + 204: + description: good diff --git a/pkg/codegen/codegen.go b/pkg/codegen/codegen.go index 7b6a15b014..5e3c489322 100644 --- a/pkg/codegen/codegen.go +++ b/pkg/codegen/codegen.go @@ -537,7 +537,7 @@ func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3. Schema: goSchema, }) - types = append(types, goSchema.GetAdditionalTypeDefs()...) + types = append(types, goSchema.AdditionalTypes...) } return types, nil } diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index dcd0262848..150e738996 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -875,11 +875,11 @@ func GenerateTypeDefsForOperation(op OperationDefinition) []TypeDefinition { // Now, go through all the additional types we need to declare. for _, param := range op.AllParams() { - typeDefs = append(typeDefs, param.Schema.GetAdditionalTypeDefs()...) + typeDefs = append(typeDefs, param.Schema.AdditionalTypes...) } for _, body := range op.Bodies { - typeDefs = append(typeDefs, body.Schema.GetAdditionalTypeDefs()...) + typeDefs = append(typeDefs, body.Schema.AdditionalTypes...) } return typeDefs } diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index dee0cdcc58..f4a7cfa363 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -76,12 +76,7 @@ func (s *Schema) AddProperty(p Property) error { } func (s Schema) GetAdditionalTypeDefs() []TypeDefinition { - var result []TypeDefinition - for _, p := range s.Properties { - result = append(result, p.Schema.GetAdditionalTypeDefs()...) - } - result = append(result, s.AdditionalTypes...) - return result + return s.AdditionalTypes } type Property struct { @@ -424,6 +419,9 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { Deprecated: p.Value.Deprecated, } outSchema.Properties = append(outSchema.Properties, prop) + if len(pSchema.AdditionalTypes) > 0 { + outSchema.AdditionalTypes = append(outSchema.AdditionalTypes, pSchema.AdditionalTypes...) + } } if schema.AnyOf != nil { @@ -457,7 +455,7 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) { Description: newTypeDef.Schema.Description, GoType: typeName, DefineViaAlias: true, - AdditionalTypes: []TypeDefinition{newTypeDef}, + AdditionalTypes: append(outSchema.AdditionalTypes, newTypeDef), } }