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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ Binary multipart parts are `Blob` values; their media types must match the
content types declared by the corresponding prefix encoding. Unsupported
request-body shapes fail generation.

Response bodies with a schema under a media type that is neither JSON, text,
XML nor an event stream are raw bytes, such as `type: string` with
`contentMediaType: application/octet-stream`. Clients return them unparsed:
`Blob` in TypeScript, `[]byte` in Go, `Vec<u8>` in Rust and `List<int>` in Dart.

## OpenTelemetry trace propagation

Generated clients leave OpenTelemetry dependencies and SDK setup to the
Expand Down
7 changes: 7 additions & 0 deletions internal/clientgen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func (response Response) Text() bool {
response.MediaType == "application/xml" || strings.HasSuffix(response.MediaType, "+xml"))
}

// Binary reports whether the response body is raw bytes: it has a schema but is
// neither a JSON, text, nor server-sent event representation. OpenAPI 3.1+
// describes such bodies as `type: string` with `contentMediaType`.
func (response Response) Binary() bool {
return response.HasBody() && !response.SSE && !response.JSON() && !response.Text()
}

// Analyze normalizes and validates every operation used by generated clients.
func Analyze(doc *openapi.Document) ([]Operation, error) {
routes := doc.Operations()
Expand Down
8 changes: 7 additions & 1 deletion internal/goemit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type goResponseData struct {
Body bool
JSON bool
Text bool
Binary bool
SSE bool
ReconnectableSSE bool
}
Expand Down Expand Up @@ -197,13 +198,18 @@ func (e *emitter) operationResponses(analyzed []clientgen.Response) ([]goRespons
responses := make([]goResponseData, 0, len(analyzed))
var bodylessStatuses []string
for _, response := range analyzed {
responseType := binaryType
if !response.Binary() {
responseType = e.goType(response.Schema)
}
responseData := goResponseData{
Status: response.Status,
FieldName: fmt.Sprintf("Status%d", response.Status),
Type: e.goType(response.Schema),
Type: responseType,
Body: response.HasBody(),
JSON: response.JSON(),
Text: response.Text(),
Binary: response.Binary(),
SSE: response.SSE,
ReconnectableSSE: response.SSE,
}
Expand Down
5 changes: 4 additions & 1 deletion internal/goemit/goemit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type Options struct {
Strict bool
}

const goInt32 = "int32"
const (
goInt32 = "int32"
binaryType = "[]byte"
)

type emitter struct{ strict bool }

Expand Down
5 changes: 3 additions & 2 deletions internal/goemit/templates/client.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,10 @@ func (c *Client) {{.ID}}(ctx context.Context{{if .HasParams}}, params {{.ParamsT
if len(rawBody) > maxDecodedBodyBytes {
return nil, fmt.Errorf("read {{$operation.ID}} status {{.Status}} response: body exceeds %d bytes", maxDecodedBodyBytes)
}
decoded := {{.Type}}(rawBody)
{{if .Binary}} result.{{.FieldName}} = &rawBody
{{else}} decoded := {{.Type}}(rawBody)
result.{{.FieldName}} = &decoded
return result, nil
{{end}} return result, nil
{{end}}{{end}}{{end}}{{if .BodylessStatuses}} case {{.BodylessStatuses}}:
_ = res.Body.Close()
return result, nil
Expand Down
2 changes: 2 additions & 0 deletions internal/tsemit/templates/api.ts.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ async function runInterceptors(
return { status: {{.Status}}, body, raw: response };
{{else if .Text}} const body: {{.Type}} = await response.text();
return { status: {{.Status}}, body, raw: response };
{{else if .Binary}} const body: {{.Type}} = await response.blob();
return { status: {{.Status}}, body, raw: response };
{{else}} return { status: {{.Status}}, raw: response };
{{end}} }
{{end}} throw new ResponseError(response, `Unexpected response status ${response.status}`);
Expand Down
11 changes: 9 additions & 2 deletions internal/tsemit/tsemit.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ type opResponse struct {
SSE bool
JSON bool
Text bool
Binary bool
}

func modelImports(doc *openapi.Document, operations []clientgen.Operation) []string {
Expand Down Expand Up @@ -444,21 +445,26 @@ func multipartBodyTemplateData(analyzed *clientgen.MultipartBody) *tsMultipartBo

func multipartPartType(part clientgen.MultipartPart) string {
if part.Binary {
return "Blob"
return binaryType
}
return tsType(part.Schema)
}

func operationResponses(analyzed []clientgen.Response) []opResponse {
responses := make([]opResponse, 0, len(analyzed))
for _, response := range analyzed {
responseType := tsType(response.Schema)
if response.Binary() {
responseType = binaryType
}
responses = append(responses, opResponse{
Status: response.Status,
Type: tsType(response.Schema),
Type: responseType,
Body: response.HasBody(),
SSE: response.SSE,
JSON: response.JSON(),
Text: response.Text(),
Binary: response.Binary(),
})
}
return responses
Expand Down Expand Up @@ -536,6 +542,7 @@ const (
nullType = "null"
jsonBodyName = "body"
rawBodyParam = "rawBody"
binaryType = "Blob"
)

func tsType(schema *openapi.Schema) string {
Expand Down
2 changes: 2 additions & 0 deletions moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ tasks:
go run ./cmd/oasmith --openapi testdata/fixtures/public-client.yaml --mode client --lang go --out testdata/golden/public-client-go
go run ./cmd/oasmith --openapi testdata/fixtures/private.yaml --mode client --lang typescript --out testdata/golden/private-typescript
go run ./cmd/oasmith --openapi testdata/fixtures/config.yaml --mode client --lang typescript --out testdata/golden/config-typescript
go run ./cmd/oasmith --openapi testdata/fixtures/binary-response.yaml --mode client --lang go --out testdata/golden/binary-response-go
go run ./cmd/oasmith --openapi testdata/fixtures/binary-response.yaml --mode client --lang typescript --out testdata/golden/binary-response-typescript
options:
cache: false
runInCI: false
Expand Down
16 changes: 16 additions & 0 deletions oasmith_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ func TestGoldenFixtures(t *testing.T) {
return tsemit.Emit(doc, tsemit.Options{OutDir: outDir})
},
},
{
name: "binary-response-go",
fixture: "binary-response.yaml",
golden: "binary-response-go",
emit: func(doc *openapi.Document, outDir string, sourcePath string) error {
return goemit.EmitClient(doc, goemit.Options{OutDir: outDir, SourcePath: sourcePath})
},
},
{
name: "binary-response-typescript",
fixture: "binary-response.yaml",
golden: "binary-response-typescript",
emit: func(doc *openapi.Document, outDir string, _ string) error {
return tsemit.Emit(doc, tsemit.Options{OutDir: outDir})
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
Expand Down
24 changes: 24 additions & 0 deletions testdata/fixtures/binary-response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.2.0
info:
title: Binary response fixture
version: "1"
paths:
/files/{key}:
get:
operationId: getFile
parameters:
- name: key
in: path
required: true
schema:
type: string
responses:
"200":
description: File bytes.
content:
application/octet-stream:
schema:
type: string
contentMediaType: application/octet-stream
"404":
description: No file with that key.
Loading