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

feat!: generate models for OpenAPI parameters #1498

Merged
merged 4 commits into from
Sep 5, 2023
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
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
) {
Comment on lines +175 to +182
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont want to introduce an option so the user can specify whether they want to include parameters or not? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameters are just as much part of the OpenAPI spec as the requests and responses, so I don't see much of a use case in having them as an option. The extra cost is minimal. Happy to add one if you have strong views. I don't see anything similar on ProcessorOptions for other document formats.

Copy link
Member

@jonaslagoni jonaslagoni Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats because in for example AsyncAPI input processor, we only generate message payloads, not parameters, even though we probably should allow it in some way (probably through options) 😄

But no I dont have any strong feelings for the OpenAPI input, so here it's fine 😄

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
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",
],
]
`;
Loading