Skip to content

Commit

Permalink
refactor(specGenerator)/remove request-prop param (#1701)
Browse files Browse the repository at this point in the history
* refactor(specgenerator): add request prop to filter

* test: schema coverage request-prop

* test(spec): allowed in cases
  • Loading branch information
chappelo authored Oct 14, 2024
1 parent 9868804 commit 4b2fa72
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/swagger/specGenerator2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class SpecGenerator2 extends SpecGenerator {

pathMethod.parameters = method.parameters
.filter(p => {
return ['request', 'body-prop', 'res', 'queries'].indexOf(p.in) === -1;
return ['request', 'body-prop', 'res', 'queries', 'request-prop'].indexOf(p.in) === -1;
})
.map(p => this.buildParameter(p));

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class SpecGenerator3 extends SpecGenerator {

pathMethod.parameters = method.parameters
.filter(p => {
return ['body', 'formData', 'request', 'body-prop', 'res', 'queries'].indexOf(p.in) === -1;
return ['body', 'formData', 'request', 'body-prop', 'res', 'queries', 'request-prop'].indexOf(p.in) === -1;
})
.map(p => this.buildParameter(p));

Expand Down
21 changes: 18 additions & 3 deletions tests/fixtures/controllers/parameterController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, BodyProp, Get, Header, Path, Post, Query, Request, Route, Res, TsoaResponse, Deprecated, Queries, RequestProp } from '@tsoa/runtime';
import { Body, BodyProp, Get, Header, Path, Post, Query, Request, Route, Res, TsoaResponse, Deprecated, Queries, RequestProp, FormField } from '@tsoa/runtime';
import { Gender, ParameterTestModel } from '../testModel';

@Route('ParameterTest')
Expand Down Expand Up @@ -308,12 +308,27 @@ export class ParameterController {
});
}

@Get('ParamaterQueyAnyType')
@Get('ParameterHeaderStringType')
public async headerStringType(@Header() header: string): Promise<void> {
//
}

@Get('FormDataStringType')
public async formData(@FormField() data: string): Promise<void> {
//
}

@Get('Path/{test}')
public async pathStringType(@Path() test: string): Promise<void> {
//
}

@Get('ParamaterQueryAnyType')
public async queryAnyType(@Query() name: any): Promise<void> {
//
}

@Post('ParamaterQueyArray')
@Post('ParamaterQueryArray')
public async queyArray(@Query() name: string[]): Promise<void> {
//
}
Expand Down
72 changes: 72 additions & 0 deletions tests/unit/swagger/schemaDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,4 +820,76 @@ describe('Schema details generation', () => {
expect(errToTest!.message).to.match(/Swagger 2.0 does not support "openIdConnect" security scheme/);
});
});

describe('should include valid params', () => {
it('should include query', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/ParamaterQueryAnyType'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('query');
});

it('should include body', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/ParamaterBodyAnyType'].post?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('body');
});

it('should include header', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/ParameterHeaderStringType'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('header');
});

it('should include path', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/Path/{test}'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('path');
});

it('should include formData', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/FormDataStringType'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('formData');
});
});

describe('should exclude @RequestProp', () => {
it('should exclude request-prop from method parameters', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/RequestProps'].post?.parameters ?? [];

expect(method).to.have.lengthOf(0);

method.forEach(p => {
expect(p.in).to.not.equal('request-prop');
});
});
});
});
50 changes: 50 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4752,4 +4752,54 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
expect(currentSpec.paths['/ParameterTest/Inline1'].post?.requestBody?.content['application/json'].schema?.title).to.equal(undefined);
});
});

describe('should include valid params', () => {
it('should include query', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/ParamaterQueryAnyType'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('query');
});

it('should include header', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/ParameterHeaderStringType'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('header');
});

it('should include path', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/Path/{test}'].get?.parameters ?? [];

expect(method).to.have.lengthOf(1);
const queryParam = method[0];
expect(queryParam.in).to.equal('path');
});
});

describe('should exclude @RequestProp', () => {
it('should exclude request-prop from method parameters', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/parameterController.ts').Generate();
const spec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();

const method = spec.paths['/ParameterTest/RequestProps'].post?.parameters ?? [];

expect(method).to.have.lengthOf(0);

method.forEach(p => {
expect(p.in).to.not.equal('request-prop');
});
});
});
});

0 comments on commit 4b2fa72

Please sign in to comment.