Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewPetClient(baseURL string, opts ...PetClientOption) (*PetClient, error)
func WithHTTPClient(d HTTPDoer) PetClientOption
func WithTimeout(d time.Duration) PetClientOption
func WithRequestEditor(fns ...RequestEditor) PetClientOption
func WithJSON(marshal func(v any) ([]byte, error), unmarshal func(data []byte, v any) error) PetClientOption
```

### Base URL
Expand Down Expand Up @@ -61,6 +62,24 @@ that returns an error stops the request. Editors are the place for credentials.

A method takes editors of its own too, see [Methods](#methods).

### JSON library

The client writes and reads JSON with `encoding/json`. `WithJSON` swaps in another library that
has the same two functions:

```go
import "github.com/bytedance/sonic"

c, err := NewPetClient(baseURL, WithJSON(sonic.Marshal, sonic.Unmarshal))
```

| Uses the library | Stays on `encoding/json` |
|---|---|
| JSON request bodies | parameters with JSON content |
| JSON response bodies and stream frames | the `MarshalJSON` methods of generated types, which the library calls |

A nil function panics at once, as a nil `HTTPDoer` does.

### Operation ID

The context of a request holds the name of its operation, `ListPets`. Editors and the `HTTPDoer`
Expand Down Expand Up @@ -552,6 +571,7 @@ return stream.Err()
| `Close()` | may be called from another goroutine to end a pending `Next`, which then returns false |
| `Sentinels` | frames that end the stream instead of being decoded, see below |
| `MaxFrameSize` | the size limit of one frame, see below |
| `Unmarshal` | what decodes a frame, the client's [JSON library](#json-library) in `<Op>Stream` and `json.Unmarshal` when nil |

#### Err

Expand Down
3 changes: 2 additions & 1 deletion docs/migration/ogen.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ are built and tested.
- The params of an operation sit in one options struct, with a field for each place they go:
`PathParams`, `Query`, `Headers` and `Cookies`. The body is `Body` in the same struct, not a
second argument.
- JSON goes through `encoding/json`, not `jx`.
- JSON goes through `encoding/json`, not `jx`. `WithJSON` on the client, and `WithJSONDecoder` and
`WithJSONEncoder` on the server, take another library such as sonic.
- A `uuid`, `uri` or `ipv4` format is a `string` checked by `Validate`, unless
`models.format-types` names a type for it.
- There is no security handler and no OpenTelemetry. A middleware checks credentials, and a
Expand Down
32 changes: 28 additions & 4 deletions docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,33 @@ Options are set with `ServerOption` functions on the adapter and on the router a
|---|---|
| `WithMiddleware(mw...)` | `func(http.Handler) http.Handler` wrappers, outermost first |
| `WithErrorHandler(h)` | what writes failed requests, `DefaultErrorHandler{}` by default |
| `WithJSONDecoder(fn)` | what reads JSON bodies, `runtime.DecodeJSON` by default |
| `WithJSONDecoder(fn)` | what reads JSON request bodies, `json.Unmarshal` by default |
| `WithJSONEncoder(fn)` | what writes JSON response bodies and stream frames, `json.Marshal` by default |
| `WithPresence(p)` | what checks the keys of request bodies and fills their defaults, see [request bodies](#request-bodies) |
| `WithMultipartMaxMemory(n)` | memory for multipart forms, `server.multipart-max-memory` by default |
| `WithRouter(r)` | the router the routes go on, one of the framework's |

### JSON library

`WithJSONDecoder` and `WithJSONEncoder` take the two functions of any library shaped like
`encoding/json`:

```go
import "github.com/bytedance/sonic"

router := NewRouter(svc,
WithJSONDecoder(sonic.Unmarshal),
WithJSONEncoder(sonic.Marshal),
)
```

The adapter still reads the body and checks it first. An empty required body is a 400 before the
decoder runs.

These stay on `encoding/json`: parameters with JSON content, the bodies `DefaultErrorHandler`
writes, the key checks of [request bodies](#request-bodies), and the `MarshalJSON` methods of
generated types, which the library calls.

## Validation

```yaml
Expand Down Expand Up @@ -1101,7 +1123,8 @@ goes as it is.

### Body codecs

- `DecodeJSON`, `DecodeText`, `DecodeBytes` and `DecodeFile`.
- `DecodeJSON` reads a body with the unmarshal function it is given. `DecodeText`, `DecodeBytes`
and `DecodeFile`.
- `DecodeForm`: bracketed keys nest, `address[city]=Berlin`, `items[0]=a`. One value for a struct
or map is read as JSON, else as a string. A property its `Encoding` gives a style is read as a
query parameter of that style.
Expand All @@ -1119,8 +1142,9 @@ goes as it is.
Five packages sit beside it:

- `pkg/runtime/httpserver` holds what a generated server needs beside the codecs: `HandlerError`
and the error handlers, and `Write`, which sends a status, headers and a body. It writes JSON for
most values, text and bytes as they are, and streams a `File`.
and the error handlers, and `Writer`, whose `Write` sends a status, headers and a body. It writes
JSON for most values with its `Marshal`, `json.Marshal` when nil, text and bytes as they are,
and streams a `File`.
- `pkg/runtime/httpclient` holds the requests and responses of a client, see
[client](client.md#runtime).
- `pkg/runtime/validation` holds the checks of `Validate` and the errors they return (see
Expand Down
23 changes: 16 additions & 7 deletions examples/bodies/checked/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions examples/bodies/defaults/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 42 additions & 15 deletions examples/bodies/encoding/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading