Skip to content

Commit

Permalink
chore: support full path schemas (#5723)
Browse files Browse the repository at this point in the history
This backwards compatible change allows us to specify a schema `id`
(full path) which to me feels a bit better than specifying the schema
name as a string, since a literal string is prone to typos.

### Before

```ts
requestBody: createRequestSchema(
    'createResourceSchema',
),
responses: {
    ...getStandardResponses(400, 401, 403, 415),
    201: resourceCreatedResponseSchema(
        'resourceSchema',
    ),
},
```

### After

```ts
requestBody: createRequestSchema(
    createResourceSchema.$id,
),
responses: {
    ...getStandardResponses(400, 401, 403, 415),
    201: resourceCreatedResponseSchema(
        resourceSchema.$id,
    ),
},
```
  • Loading branch information
nunogois authored Dec 22, 2023
1 parent c44601b commit fb94138
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/lib/openapi/util/create-request-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const createRequestSchema = (
content: {
'application/json': {
schema: {
$ref: `#/components/schemas/${schemaName}`,
$ref: schemaName.startsWith('#')
? schemaName
: `#/components/schemas/${schemaName}`,
},
},
},
Expand Down
8 changes: 6 additions & 2 deletions src/lib/openapi/util/create-response-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export const createResponseSchemas = (
export const schemaNamed = (schemaName: string): OpenAPIV3.MediaTypeObject => {
return {
schema: {
$ref: `#/components/schemas/${schemaName}`,
$ref: schemaName.startsWith('#')
? schemaName
: `#/components/schemas/${schemaName}`,
},
};
};
Expand Down Expand Up @@ -62,7 +64,9 @@ export const resourceCreatedResponseSchema = (
content: {
'application/json': {
schema: {
$ref: `#/components/schemas/${schemaName}`,
$ref: schemaName.startsWith('#')
? schemaName
: `#/components/schemas/${schemaName}`,
},
},
},
Expand Down

0 comments on commit fb94138

Please sign in to comment.