Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(type-safe-api): generate models for inline request body schemas #873

Merged
merged 1 commit into from
Oct 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -718,21 +718,34 @@ const buildData = async (inSpec: OpenAPIV3.Document, metadata: any) => {
};
}

// "Hoist" inline response schemas
// "Hoist" inline request and response schemas
Object.entries(spec.paths ?? {}).forEach(([path, pathOps]) => Object.entries(pathOps ?? {}).forEach(([method, op]) => {
const operation = resolveIfRef(spec, op);
if (operation && typeof operation === "object" && "responses" in operation) {
Object.entries(operation.responses ?? {}).forEach(([code, res]) => {
const response = resolveIfRef(spec, res);
const jsonResponseSchema = response?.content?.['application/json']?.schema;
if (jsonResponseSchema && !isRef(jsonResponseSchema) && ["object", "array"].includes(jsonResponseSchema.type!)) {
const schemaName = `${_upperFirst(_camelCase(operation.operationId ?? `${path}-${method}`))}${code}Response`;
spec.components!.schemas![schemaName] = jsonResponseSchema;
response!.content!['application/json'].schema = {
$ref: `#/components/schemas/${schemaName}`,
};
if (operation && typeof operation === "object") {
if ("responses" in operation) {
Object.entries(operation.responses ?? {}).forEach(([code, res]) => {
const response = resolveIfRef(spec, res);
const jsonResponseSchema = response?.content?.['application/json']?.schema;
if (jsonResponseSchema && !isRef(jsonResponseSchema) && ["object", "array"].includes(jsonResponseSchema.type!)) {
const schemaName = `${_upperFirst(_camelCase(operation.operationId ?? `${path}-${method}`))}${code}Response`;
spec.components!.schemas![schemaName] = jsonResponseSchema;
response!.content!['application/json'].schema = {
$ref: `#/components/schemas/${schemaName}`,
};
}
});
}
if ("requestBody" in operation) {
const requestBody = resolveIfRef(spec, operation.requestBody);
const jsonRequestSchema = requestBody?.content?.['application/json']?.schema;
if (jsonRequestSchema && !isRef(jsonRequestSchema) && ["object", "array"].includes(jsonRequestSchema.type!)) {
const schemaName = `${_upperFirst(_camelCase(operation.operationId ?? `${path}-${method}`))}RequestContent`;
spec.components!.schemas![schemaName] = jsonRequestSchema;
requestBody!.content!['application/json'].schema = {
$ref: `#/components/schemas/${schemaName}`,
};
}
});
}
}
}));

Expand Down
17 changes: 17 additions & 0 deletions packages/type-safe-api/test/resources/specs/edge-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ paths:
enum:
- fruit
- vegetable
/inline-request-body:
post:
operationId: inlineRequestBody
responses:
204:
description: ok
requestBody:
content:
application/json:
schema:
type: object
properties:
someProperty:
type:
string
required:
- someProperty
components:
schemas:
MyEnum:
Expand Down
Loading
Loading