From 46549a921860b3fedc06595a77081148c3f77788 Mon Sep 17 00:00:00 2001 From: Alex Mammay Date: Fri, 10 Feb 2023 16:15:12 -0500 Subject: [PATCH 1/3] issue958 --- internal/test/issues/issue-958/config.yaml | 8 + internal/test/issues/issue-958/doc.go | 3 + internal/test/issues/issue-958/issue.gen.go | 234 ++++++++++++++++++ .../test/issues/issue-958/pkga/config.yaml | 9 + internal/test/issues/issue-958/pkga/doc.go | 3 + .../issues/issue-958/pkga/responses.gen.go | 21 ++ internal/test/issues/issue-958/pkga/spec.yaml | 45 ++++ internal/test/issues/issue-958/spec.yaml | 14 ++ 8 files changed, 337 insertions(+) create mode 100644 internal/test/issues/issue-958/config.yaml create mode 100644 internal/test/issues/issue-958/doc.go create mode 100644 internal/test/issues/issue-958/issue.gen.go create mode 100644 internal/test/issues/issue-958/pkga/config.yaml create mode 100644 internal/test/issues/issue-958/pkga/doc.go create mode 100644 internal/test/issues/issue-958/pkga/responses.gen.go create mode 100644 internal/test/issues/issue-958/pkga/spec.yaml create mode 100644 internal/test/issues/issue-958/spec.yaml diff --git a/internal/test/issues/issue-958/config.yaml b/internal/test/issues/issue-958/config.yaml new file mode 100644 index 0000000000..1cf2e1966b --- /dev/null +++ b/internal/test/issues/issue-958/config.yaml @@ -0,0 +1,8 @@ +package: issue958 +generate: + client: true +output: issue.gen.go +output-options: + skip-prune: true +import-mapping: + ./pkga/spec.yaml: github.com/deepmap/oapi-codegen/internal/test/issues/issue-958/pkga diff --git a/internal/test/issues/issue-958/doc.go b/internal/test/issues/issue-958/doc.go new file mode 100644 index 0000000000..3be1f22d5e --- /dev/null +++ b/internal/test/issues/issue-958/doc.go @@ -0,0 +1,3 @@ +package issue958 + +//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/issues/issue-958/issue.gen.go b/internal/test/issues/issue-958/issue.gen.go new file mode 100644 index 0000000000..088b3f60b8 --- /dev/null +++ b/internal/test/issues/issue-958/issue.gen.go @@ -0,0 +1,234 @@ +// Package issue958 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT. +package issue958 + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strings" +) + +// 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 { + // ExampleGet request + ExampleGet(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) +} + +func (c *Client) ExampleGet(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewExampleGetRequest(c.Server) + 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) +} + +// NewExampleGetRequest generates requests for ExampleGet +func NewExampleGetRequest(server string) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/example") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), nil) + if err != nil { + return nil, err + } + + 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 { + // ExampleGet request + ExampleGetWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ExampleGetResponse, error) +} + +type ExampleGetResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *Document +} + +// Status returns HTTPResponse.Status +func (r ExampleGetResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r ExampleGetResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +// ExampleGetWithResponse request returning *ExampleGetResponse +func (c *ClientWithResponses) ExampleGetWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ExampleGetResponse, error) { + rsp, err := c.ExampleGet(ctx, reqEditors...) + if err != nil { + return nil, err + } + return ParseExampleGetResponse(rsp) +} + +// ParseExampleGetResponse parses an HTTP response from a ExampleGetWithResponse call +func ParseExampleGetResponse(rsp *http.Response) (*ExampleGetResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &ExampleGetResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest Document + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + } + + return response, nil +} diff --git a/internal/test/issues/issue-958/pkga/config.yaml b/internal/test/issues/issue-958/pkga/config.yaml new file mode 100644 index 0000000000..25f36109a1 --- /dev/null +++ b/internal/test/issues/issue-958/pkga/config.yaml @@ -0,0 +1,9 @@ +package: pkga +generate: + echo-server: false + client: false + models: true + embedded-spec: false +output-options: + skip-prune: true +output: responses.gen.go diff --git a/internal/test/issues/issue-958/pkga/doc.go b/internal/test/issues/issue-958/pkga/doc.go new file mode 100644 index 0000000000..92d00ebabe --- /dev/null +++ b/internal/test/issues/issue-958/pkga/doc.go @@ -0,0 +1,3 @@ +package pkga + +//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/issues/issue-958/pkga/responses.gen.go b/internal/test/issues/issue-958/pkga/responses.gen.go new file mode 100644 index 0000000000..81d18b3154 --- /dev/null +++ b/internal/test/issues/issue-958/pkga/responses.gen.go @@ -0,0 +1,21 @@ +// Package pkga provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT. +package pkga + +// ArrayValue defines model for ArrayValue. +type ArrayValue = []Value + +// Document defines model for Document. +type Document struct { + Fields *map[string]Value `json:"fields,omitempty"` +} + +// Value defines model for Value. +type Value struct { + ArrayValue *ArrayValue `json:"arrayValue,omitempty"` + StringValue *string `json:"stringValue,omitempty"` +} + +// N200 defines model for 200. +type N200 = Document diff --git a/internal/test/issues/issue-958/pkga/spec.yaml b/internal/test/issues/issue-958/pkga/spec.yaml new file mode 100644 index 0000000000..3fc5ea0900 --- /dev/null +++ b/internal/test/issues/issue-958/pkga/spec.yaml @@ -0,0 +1,45 @@ +openapi: 3.0.2 +info: + version: '0.0.1' + title: example + description: | + Make sure client with responses can reference external responses +paths: + /example: + get: + operationId: exampleGet + responses: + '200': + description: "OK" + content: + 'application/json': + schema: + $ref: '#/components/schemas/Document' +components: + responses: + 200: + description: "OK" + content: + 'application/json': + schema: + $ref: '#/components/schemas/Document' + schemas: + Document: + type: object + properties: + fields: + type: object + additionalProperties: + $ref: '#/components/schemas/Value' + Value: + type: object + properties: + stringValue: + type: string + arrayValue: + $ref: '#/components/schemas/ArrayValue' + ArrayValue: + type: array + items: + $ref: '#/components/schemas/Value' + diff --git a/internal/test/issues/issue-958/spec.yaml b/internal/test/issues/issue-958/spec.yaml new file mode 100644 index 0000000000..685ab4fbaf --- /dev/null +++ b/internal/test/issues/issue-958/spec.yaml @@ -0,0 +1,14 @@ +openapi: 3.0.2 +info: + version: '0.0.1' + title: example + description: | + Make sure client with responses can reference external responses +paths: + /example: + get: + operationId: exampleGet + responses: + 200: + $ref: "./pkga/spec.yaml#/components/responses/200" + From ed085aea853f07d14012e523bcbb499ea51f74d9 Mon Sep 17 00:00:00 2001 From: Ryan Marken Date: Tue, 7 Mar 2023 12:56:09 -0500 Subject: [PATCH 2/3] String manipulation --- internal/test/issues/issue-958/issue.gen.go | 8 +++++--- pkg/codegen/operations.go | 9 ++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/test/issues/issue-958/issue.gen.go b/internal/test/issues/issue-958/issue.gen.go index 088b3f60b8..bfaa1763aa 100644 --- a/internal/test/issues/issue-958/issue.gen.go +++ b/internal/test/issues/issue-958/issue.gen.go @@ -1,6 +1,6 @@ // Package issue958 provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT. +// Code generated by unknown module path version unknown version DO NOT EDIT. package issue958 import ( @@ -11,6 +11,8 @@ import ( "net/http" "net/url" "strings" + + externalRef0 "github.com/deepmap/oapi-codegen/internal/test/issues/issue-958/pkga" ) // RequestEditorFn is the function signature for the RequestEditor callback function @@ -179,7 +181,7 @@ type ClientWithResponsesInterface interface { type ExampleGetResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *Document + JSON200 *externalRef0.Document } // Status returns HTTPResponse.Status @@ -222,7 +224,7 @@ func ParseExampleGetResponse(rsp *http.Response) (*ExampleGetResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest Document + var dest externalRef0.Document if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index 314e89462d..65f8130c49 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -278,14 +278,21 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefini sortedResponsesKeys := SortedResponsesKeys(responses) for _, responseName := range sortedResponsesKeys { responseRef := responses[responseName] - + refParts := strings.Split(responseRef.Ref, "#") + _, isExternalImport := importMapping[refParts[0]] // We can only generate a type if we have a value: if responseRef.Value != nil { sortedContentKeys := SortedContentKeys(responseRef.Value.Content) for _, contentTypeName := range sortedContentKeys { contentType := responseRef.Value.Content[contentTypeName] // We can only generate a type if we have a schema: + schemaCopy := contentType.Schema + schemaParts := strings.Split(schemaCopy.Ref, "#") if contentType.Schema != nil { + if isExternalImport { + refDef := schemaParts[len(schemaParts)-1] + schemaCopy.Ref = fmt.Sprintf("%s#%s", refParts[0], refDef) + } responseSchema, err := GenerateGoSchema(contentType.Schema, []string{responseName}) if err != nil { return nil, fmt.Errorf("Unable to determine Go type for %s.%s: %w", o.OperationId, contentTypeName, err) From 1c50addfcf66b4992fa37587d039f4de5ef0608f Mon Sep 17 00:00:00 2001 From: Ryan Marken Date: Tue, 7 Mar 2023 14:06:24 -0500 Subject: [PATCH 3/3] Adjust logic to provide external refs --- pkg/codegen/operations.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index 65f8130c49..5a5f4d68c9 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -279,6 +279,7 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefini for _, responseName := range sortedResponsesKeys { responseRef := responses[responseName] refParts := strings.Split(responseRef.Ref, "#") + // Checking to see if reference belongs to external doc so child content can be adjusted. _, isExternalImport := importMapping[refParts[0]] // We can only generate a type if we have a value: if responseRef.Value != nil { @@ -286,14 +287,16 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefini for _, contentTypeName := range sortedContentKeys { contentType := responseRef.Value.Content[contentTypeName] // We can only generate a type if we have a schema: - schemaCopy := contentType.Schema - schemaParts := strings.Split(schemaCopy.Ref, "#") if contentType.Schema != nil { - if isExternalImport { + // Create a copy as to not disturb original. + schemaCopy := *contentType.Schema + // When an external reference is provided, adjust the ref to contain full path so imports can be attached. + if isExternalImport && IsGoTypeReference(schemaCopy.Ref) { + schemaParts := strings.Split(schemaCopy.Ref, "#") refDef := schemaParts[len(schemaParts)-1] schemaCopy.Ref = fmt.Sprintf("%s#%s", refParts[0], refDef) } - responseSchema, err := GenerateGoSchema(contentType.Schema, []string{responseName}) + responseSchema, err := GenerateGoSchema(&schemaCopy, []string{responseName}) if err != nil { return nil, fmt.Errorf("Unable to determine Go type for %s.%s: %w", o.OperationId, contentTypeName, err) } @@ -320,13 +323,6 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefini ResponseName: responseName, ContentTypeName: contentTypeName, } - if IsGoTypeReference(contentType.Schema.Ref) { - refType, err := RefPathToGoType(contentType.Schema.Ref) - if err != nil { - return nil, fmt.Errorf("error dereferencing response Ref: %w", err) - } - td.Schema.RefType = refType - } tds = append(tds, td) } }