Skip to content

Commit

Permalink
feat!: generate models for OpenAPI parameters (#1498)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Kenyon-Jones <[email protected]>
  • Loading branch information
dlkj and danielkenyonjonesfs authored Sep 5, 2023
1 parent bdd9cb4 commit 32e156f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/processors/OpenAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export class OpenAPIInputProcessor extends AbstractInputProcessor {
inputModel,
options
);
this.iterateParameters(
pathObject.parameters,
`${formattedPathName}_parameters`,
inputModel,
options
);
}
}

Expand All @@ -112,6 +118,12 @@ export class OpenAPIInputProcessor extends AbstractInputProcessor {
) {
if (operation) {
this.iterateResponses(operation.responses, path, inputModel, options);
this.iterateParameters(
operation.parameters,
`${path}_parameters`,
inputModel,
options
);

if (operation.requestBody) {
this.iterateMediaType(
Expand Down Expand Up @@ -160,6 +172,27 @@ export class OpenAPIInputProcessor extends AbstractInputProcessor {
}
}

private iterateParameters(
parameters:
| (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[]
| undefined,
path: string,
inputModel: InputMetaModel,
options?: ProcessorOptions
) {
for (const parameterObject of parameters || []) {
const parameter = parameterObject as OpenAPIV3.ParameterObject;
if (parameter.schema) {
this.includeSchema(
parameter.schema as OpenAPIV3.SchemaObject,
`${path}_${parameter.in}_${parameter.name}`,
inputModel,
options
);
}
}
}

private iterateMediaType(
mediaTypes: { [media: string]: OpenAPIV3.MediaTypeObject },
path: string,
Expand Down
54 changes: 48 additions & 6 deletions test/processors/OpenAPIInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const basicDoc = JSON.parse(
)
);
jest.mock('../../src/utils/LoggingInterface');
jest.spyOn(OpenAPIInputProcessor, 'convertToInternalSchema');
const processorSpy = jest.spyOn(
OpenAPIInputProcessor,
'convertToInternalSchema'
);
const mockedReturnModels = [new CommonModel()];
const mockedMetaModel = new AnyModel('', undefined);
jest.mock('../../src/helpers/CommonModelToMetaModel', () => {
Expand All @@ -34,6 +37,9 @@ describe('OpenAPIInputProcessor', () => {
afterAll(() => {
jest.restoreAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});
describe('shouldProcess()', () => {
const processor = new OpenAPIInputProcessor();
test('should be able to process OpenAPI 3.0.0 documents', () => {
Expand Down Expand Up @@ -82,11 +88,47 @@ describe('OpenAPIInputProcessor', () => {
const processor = new OpenAPIInputProcessor();
const commonInputModel = await processor.process(basicDoc);
expect(commonInputModel).toMatchSnapshot();
expect(
(
OpenAPIInputProcessor.convertToInternalSchema as any as jest.SpyInstance
).mock.calls
).toMatchSnapshot();
expect(processorSpy.mock.calls).toMatchSnapshot();
});
test('should include schema for parameters', async () => {
const doc = {
openapi: '3.0.3',
info: {},
paths: {
'/test': {
parameters: [
{
name: 'path_parameter',
in: 'header',
schema: { type: 'string' }
}
],
get: {
parameters: [
{
name: 'operation_parameter',
in: 'query',
schema: { type: 'string' }
}
],
responses: {
204: {}
}
}
}
}
};

const processor = new OpenAPIInputProcessor();
await processor.process(doc);
expect(processorSpy.mock.calls).toContainEqual([
{ type: 'string' },
'test_get_parameters_query_operation_parameter'
]);
expect(processorSpy.mock.calls).toContainEqual([
{ type: 'string' },
'test_parameters_header_path_parameter'
]);
});
});
});
11 changes: 11 additions & 0 deletions test/processors/OpenAPIInputProcessor/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
},
"paths": {
"/test": {
"parameters": [
{
"name": "Authorization",
"in": "header",
"required": true,
"description": "credentials that authenticate a user agent with a server",
"schema": {
"type": "string"
}
}
],
"post": {
"parameters": [
{
Expand Down
24 changes: 24 additions & 0 deletions test/processors/__snapshots__/OpenAPIInputProcessor.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ InputMetaModel {
},
},
},
"parameters": Array [
Object {
"description": "credentials that authenticate a user agent with a server",
"in": "header",
"name": "Authorization",
"required": true,
"schema": Object {
"type": "string",
},
},
],
"patch": Object {
"requestBody": Object {
"content": Object {
Expand Down Expand Up @@ -552,6 +563,13 @@ Array [
},
"test_post_200_application_json",
],
Array [
Object {
"format": "uri",
"type": "string",
},
"test_post_parameters_query_callbackUrl",
],
Array [
Object {
"properties": Object {
Expand Down Expand Up @@ -768,5 +786,11 @@ Array [
},
"test_trace_application_json",
],
Array [
Object {
"type": "string",
},
"test_parameters_header_Authorization",
],
]
`;

0 comments on commit 32e156f

Please sign in to comment.