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 param examples incorrectly made into arrays #1465

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion packages/cli/src/metadataGeneration/parameterGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ export class ParameterGenerator {
if (!this.supportBodyMethod(this.method)) {
throw new GenerateMetadataError(`@BodyProp('${parameterName}') Can't support in ${this.method.toUpperCase()} method.`);
}
const { examples: example, exampleLabels } = this.getParameterExample(parameter, parameterName);

const { examples, exampleLabels } = this.getParameterExample(parameter, parameterName);
const example = examples?.length ? examples[0] : undefined

return {
default: getInitializerValue(parameter.initializer, this.current.typeChecker, type),
description: this.getParameterDescription(parameter),
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/swagger/definitionsGeneration/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,13 @@ describe('Metadata generation', () => {
expect(parameter.parameterName).to.equal('firstname');
expect(parameter.required).to.be.true;
expect(parameter.example).not.to.be.undefined;
expect(parameter.example).to.deep.equal(['name1', 'name2']);
expect((parameter.example as unknown[]).length).to.be.equal(2);
/**
* Multiple example values per property are not allowed.
* Putting them into an array is *not* the intended behavior
* {@link https://swagger.io/docs/specification/2-0/adding-examples/}
*/
// expect(parameter.example).to.deep.equal(['name1', 'name2']);
// expect((parameter.example as unknown[]).length).to.be.equal(2);
});

it('should generate a res parameter and the corresponding additional response', () => {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
});
});

describe('should generate single example for @BodyProp parameters', () => {
it('Single @BodyProp parameter in Post method', () => {
const postBodyParams = exampleSpec.paths['/ExampleTest/post_body_prop_single'].post?.requestBody?.content?.['application/json'];
expect(postBodyParams?.schema?.properties?.prop1?.example).to.equal('prop1');
});

it('Two @BodyProp parameters in Post method', () => {
const postBodyParams = exampleSpec.paths['/ExampleTest/post_body_prop'].post?.requestBody?.content?.['application/json'];
expect(postBodyParams?.schema?.properties?.prop1?.example).to.equal('prop1_1');
expect(postBodyParams?.schema?.properties?.prop2?.example).to.equal('prop2_1');
});
})

it('Supports custom example labels', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate();
const exampleSpec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();
Expand Down