Skip to content

Commit e672ee4

Browse files
authored
Merge pull request #1397 from tdakkota/fix/pass-request-options
fix(gen): pass request options
2 parents f9825a1 + 9eacdc6 commit e672ee4

20 files changed

+1287
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"openapi": "3.0.3",
3+
"paths": {
4+
"/foo": {
5+
"get": {
6+
"operationId": "Foo",
7+
"parameters": [
8+
{
9+
"name": "body",
10+
"in": "query",
11+
"required": true,
12+
"schema": {
13+
"type": "string"
14+
}
15+
}
16+
],
17+
"responses": {
18+
"200": {
19+
"content": {
20+
"application/json": {
21+
"schema": {
22+
"type": "string"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}

gen/_template/client.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func WithRequestClient(client ht.Client) RequestOption {
4040
}
4141
}
4242

43+
// WithServerURL sets client for request.
44+
func WithServerURL(u *url.URL) RequestOption {
45+
return func(cfg *requestConfig) {
46+
cfg.ServerURL = u
47+
}
48+
}
49+
4350
// WithEditRequest sets function to edit request.
4451
func WithEditRequest(fn func(req *http.Request) error) RequestOption {
4552
return func(cfg *requestConfig) {
@@ -205,6 +212,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) {{ $op.Name }}(ctx cont
205212
{{- if $op.WebhookInfo }},targetURL{{ end -}}
206213
{{- if $op.Request }},request{{ end -}}
207214
{{- if $op.Params }},params{{ end -}}
215+
{{- if $cfg.RequestOptionsEnabled }},options...{{ end -}}
208216
)
209217
return {{ if $op.Responses.DoPass }}res,{{ end }} err
210218
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generator:
2+
features:
3+
enable:
4+
- "client/request/options"
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
9+
"net/url"
10+
"strings"
11+
"testing"
12+
13+
ht "github.com/ogen-go/ogen/http"
14+
api "github.com/ogen-go/ogen/internal/integration/test_client_options"
15+
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
type clientOptionsHandler struct{}
20+
21+
// Foo implements api.Handler.
22+
func (c *clientOptionsHandler) Foo(ctx context.Context, params api.FooParams) (string, error) {
23+
return params.Body, nil
24+
}
25+
26+
var _ api.Handler = (*clientOptionsHandler)(nil)
27+
28+
func TestClientOptions(t *testing.T) {
29+
h, err := api.NewServer(&clientOptionsHandler{})
30+
require.NoError(t, err)
31+
32+
s := httptest.NewServer(h)
33+
defer s.Close()
34+
35+
t.Run("WithRequestClient", func(t *testing.T) {
36+
ctx := context.Background()
37+
38+
c, err := api.NewClient(s.URL)
39+
require.NoError(t, err)
40+
41+
op := api.WithRequestClient(new(testFaultyClient))
42+
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
43+
require.ErrorContains(t, err, `test faulty client`)
44+
})
45+
t.Run("WithServerURL", func(t *testing.T) {
46+
ctx := context.Background()
47+
48+
c, err := api.NewClient(`http://completly-wrong-url.foo`)
49+
require.NoError(t, err)
50+
51+
u, err := url.Parse(s.URL)
52+
require.NoError(t, err)
53+
54+
op := api.WithServerURL(u)
55+
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
56+
require.NoError(t, err)
57+
require.Equal(t, "test", resp)
58+
})
59+
t.Run("WithEditRequest", func(t *testing.T) {
60+
ctx := context.Background()
61+
62+
c, err := api.NewClient(s.URL)
63+
require.NoError(t, err)
64+
65+
op := api.WithEditRequest(func(req *http.Request) error {
66+
q := req.URL.Query()
67+
q.Set("body", "request-override")
68+
req.URL.RawQuery = q.Encode()
69+
return nil
70+
})
71+
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
72+
require.NoError(t, err)
73+
require.Equal(t, "request-override", resp)
74+
75+
op = api.WithEditRequest(func(*http.Request) error {
76+
return errors.New("request editor error")
77+
})
78+
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
79+
require.ErrorContains(t, err, `request editor error`)
80+
})
81+
t.Run("WithEditResponse", func(t *testing.T) {
82+
ctx := context.Background()
83+
84+
c, err := api.NewClient(s.URL)
85+
require.NoError(t, err)
86+
87+
op := api.WithEditResponse(func(resp *http.Response) error {
88+
resp.Body = io.NopCloser(strings.NewReader(`"response-override"`))
89+
return nil
90+
})
91+
resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op)
92+
require.NoError(t, err)
93+
require.Equal(t, "response-override", resp)
94+
95+
op = api.WithEditResponse(func(*http.Response) error {
96+
return errors.New("response editor error")
97+
})
98+
_, err = c.Foo(ctx, api.FooParams{Body: "test"}, op)
99+
require.ErrorContains(t, err, `response editor error`)
100+
})
101+
}
102+
103+
type testFaultyClient struct{}
104+
105+
var _ ht.Client = (*testFaultyClient)(nil)
106+
107+
func (f *testFaultyClient) Do(req *http.Request) (*http.Response, error) {
108+
return nil, errors.New("test faulty client")
109+
}

internal/integration/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ package integration
3030
//go:generate go run ../../cmd/ogen -v --clean --config _config/allOf.yml --target test_allof ../../_testdata/positive/allOf.yml
3131
//go:generate go run ../../cmd/ogen -v --clean --config _config/anyOf.yml --target test_anyof ../../_testdata/positive/anyOf.json
3232
//go:generate go run ../../cmd/ogen -v --clean --config _config/additionalPropertiesPatternProperties.yml --target test_additionalpropertiespatternproperties ../../_testdata/positive/additionalPropertiesPatternProperties.yml
33+
//go:generate go run ../../cmd/ogen -v --clean --config _config/client_options.yml --target test_client_options ../../_testdata/positive/client_options.json
3334
//
3435
//go:generate go run ../../cmd/ogen -v --clean -target test_enum_naming ../../_testdata/positive/enum_naming.yml
3536
//go:generate go run ../../cmd/ogen -v --clean -target test_naming_extensions ../../_testdata/positive/naming_extensions.json

0 commit comments

Comments
 (0)