Skip to content

Commit

Permalink
fix: append type object prop to generated parameter schemas (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign authored Feb 27, 2024
1 parent a134798 commit fcd7b9d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-students-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-json-schema": patch
---

Append `type: "object"` prop to generated parameter schemas
24 changes: 22 additions & 2 deletions src/utils/convertOpenApiParameters.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { convertParametersToJSONSchema } from 'openapi-jsonschema-parameters';
import {
convertParametersToJSONSchema as _convertParametersToJSONSchema,
OpenAPIParametersAsJSONSchema,
} from 'openapi-jsonschema-parameters';
import type { JSONSchema } from '../types';

type OpenAPIParameters = Parameters<typeof _convertParametersToJSONSchema>[0];
function convertParametersToJSONSchema(
openApiParameters: OpenAPIParameters,
): OpenAPIParametersAsJSONSchema {
const parameters = _convertParametersToJSONSchema(openApiParameters);

// Append "type" prop which "openapi-jsonschema-parameters" seems to omit
let paramName: keyof OpenAPIParametersAsJSONSchema;
for (paramName in parameters) {
const schema = parameters[paramName];
if (schema && !schema.type) {
schema.type = 'object';
}
}
return parameters;
}

/**
* Parameters field can only be found in:
* - paths[path].parameters
* - paths[path][operation].parameters
*
* https://swagger.io/docs/specification/describing-parameters/
*/
export function convertOpenApiParameters(schema: JSONSchema) {
export function convertOpenApiParameters(schema: JSONSchema): JSONSchema {
if ('paths' in schema) {
for (const path in schema.paths) {
const pathSchema = schema.paths[path];
Expand Down
4 changes: 4 additions & 0 deletions test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('OpenAPI parameters', () => {
expect(pathSchema.default).toEqual({
parameters: {
headers: {
type: 'object',
required: ['path-headers-param-1'],
properties: {
'path-headers-param-1': {
Expand All @@ -30,6 +31,7 @@ describe('OpenAPI parameters', () => {
get: {
parameters: {
headers: {
type: 'object',
properties: {
'headers-param-1': {
type: 'string',
Expand All @@ -43,6 +45,7 @@ describe('OpenAPI parameters', () => {
},
body: { type: 'string', enum: ['foo', 'bar'] },
path: {
type: 'object',
properties: {
'path-param-1': {
type: 'string',
Expand All @@ -51,6 +54,7 @@ describe('OpenAPI parameters', () => {
required: ['path-param-1'],
},
query: {
type: 'object',
properties: {
'query-param-1': {
type: 'string',
Expand Down

0 comments on commit fcd7b9d

Please sign in to comment.