Skip to content

Commit

Permalink
Fix param examples incorrectly made into arrays
Browse files Browse the repository at this point in the history
fixes #1464
  • Loading branch information
Pablo Tellería committed Aug 12, 2023
1 parent 6c3d4e1 commit 898060d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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

0 comments on commit 898060d

Please sign in to comment.