Skip to content

Commit

Permalink
Merge pull request #1397 from tdakkota/fix/pass-request-options
Browse files Browse the repository at this point in the history
fix(gen): pass request options
  • Loading branch information
ernado authored Feb 10, 2025
2 parents f9825a1 + 9eacdc6 commit e672ee4
Show file tree
Hide file tree
Showing 20 changed files with 1,287 additions and 0 deletions.
31 changes: 31 additions & 0 deletions _testdata/positive/client_options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"openapi": "3.0.3",
"paths": {
"/foo": {
"get": {
"operationId": "Foo",
"parameters": [
{
"name": "body",
"in": "query",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions gen/_template/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func WithRequestClient(client ht.Client) RequestOption {
}
}

// WithServerURL sets client for request.
func WithServerURL(u *url.URL) RequestOption {
return func(cfg *requestConfig) {
cfg.ServerURL = u
}
}

// WithEditRequest sets function to edit request.
func WithEditRequest(fn func(req *http.Request) error) RequestOption {
return func(cfg *requestConfig) {
Expand Down Expand Up @@ -205,6 +212,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) {{ $op.Name }}(ctx cont
{{- if $op.WebhookInfo }},targetURL{{ end -}}
{{- if $op.Request }},request{{ end -}}
{{- if $op.Params }},params{{ end -}}
{{- if $cfg.RequestOptionsEnabled }},options...{{ end -}}
)
return {{ if $op.Responses.DoPass }}res,{{ end }} err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/_config/client_options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generator:
features:
enable:
- "client/request/options"
109 changes: 109 additions & 0 deletions internal/integration/client_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package integration

import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

ht "github.com/ogen-go/ogen/http"
api "github.com/ogen-go/ogen/internal/integration/test_client_options"

"github.com/stretchr/testify/require"
)

type clientOptionsHandler struct{}

// Foo implements api.Handler.
func (c *clientOptionsHandler) Foo(ctx context.Context, params api.FooParams) (string, error) {
return params.Body, nil
}

var _ api.Handler = (*clientOptionsHandler)(nil)

func TestClientOptions(t *testing.T) {
h, err := api.NewServer(&clientOptionsHandler{})
require.NoError(t, err)

s := httptest.NewServer(h)
defer s.Close()

t.Run("WithRequestClient", func(t *testing.T) {
ctx := context.Background()

c, err := api.NewClient(s.URL)
require.NoError(t, err)

op := api.WithRequestClient(new(testFaultyClient))
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.ErrorContains(t, err, `test faulty client`)
})
t.Run("WithServerURL", func(t *testing.T) {
ctx := context.Background()

c, err := api.NewClient(`http://completly-wrong-url.foo`)
require.NoError(t, err)

u, err := url.Parse(s.URL)
require.NoError(t, err)

op := api.WithServerURL(u)
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.NoError(t, err)
require.Equal(t, "test", resp)
})
t.Run("WithEditRequest", func(t *testing.T) {
ctx := context.Background()

c, err := api.NewClient(s.URL)
require.NoError(t, err)

op := api.WithEditRequest(func(req *http.Request) error {
q := req.URL.Query()
q.Set("body", "request-override")
req.URL.RawQuery = q.Encode()
return nil
})
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.NoError(t, err)
require.Equal(t, "request-override", resp)

op = api.WithEditRequest(func(*http.Request) error {
return errors.New("request editor error")
})
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.ErrorContains(t, err, `request editor error`)
})
t.Run("WithEditResponse", func(t *testing.T) {
ctx := context.Background()

c, err := api.NewClient(s.URL)
require.NoError(t, err)

op := api.WithEditResponse(func(resp *http.Response) error {
resp.Body = io.NopCloser(strings.NewReader(`"response-override"`))
return nil
})
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.NoError(t, err)
require.Equal(t, "response-override", resp)

op = api.WithEditResponse(func(*http.Response) error {
return errors.New("response editor error")
})
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
require.ErrorContains(t, err, `response editor error`)
})
}

type testFaultyClient struct{}

var _ ht.Client = (*testFaultyClient)(nil)

func (f *testFaultyClient) Do(req *http.Request) (*http.Response, error) {
return nil, errors.New("test faulty client")
}
1 change: 1 addition & 0 deletions internal/integration/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package integration
//go:generate go run ../../cmd/ogen -v --clean --config _config/allOf.yml --target test_allof ../../_testdata/positive/allOf.yml
//go:generate go run ../../cmd/ogen -v --clean --config _config/anyOf.yml --target test_anyof ../../_testdata/positive/anyOf.json
//go:generate go run ../../cmd/ogen -v --clean --config _config/additionalPropertiesPatternProperties.yml --target test_additionalpropertiespatternproperties ../../_testdata/positive/additionalPropertiesPatternProperties.yml
//go:generate go run ../../cmd/ogen -v --clean --config _config/client_options.yml --target test_client_options ../../_testdata/positive/client_options.json
//
//go:generate go run ../../cmd/ogen -v --clean -target test_enum_naming ../../_testdata/positive/enum_naming.yml
//go:generate go run ../../cmd/ogen -v --clean -target test_naming_extensions ../../_testdata/positive/naming_extensions.json
Expand Down
Loading

0 comments on commit e672ee4

Please sign in to comment.