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/1289: values with operator is not properly handled @ decorators #1319

Merged
merged 2 commits into from
Oct 7, 2022
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
11 changes: 11 additions & 0 deletions packages/cli/src/metadataGeneration/initializer-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export const getInitializerValue = (initializer?: ts.Expression | ts.ImportSpeci
return true;
case ts.SyntaxKind.FalseKeyword:
return false;
case ts.SyntaxKind.PrefixUnaryExpression: {
const prefixUnary = initializer as ts.PrefixUnaryExpression;
switch (prefixUnary.operator) {
case ts.SyntaxKind.PlusToken:
return Number((prefixUnary.operand as ts.NumericLiteral).text);
case ts.SyntaxKind.MinusToken:
return Number(`-${(prefixUnary.operand as ts.NumericLiteral).text}`);
default:
throw new Error(`Unsupport prefix operator token: ${prefixUnary.operator}`);
}
}
case ts.SyntaxKind.NumberKeyword:
case ts.SyntaxKind.FirstLiteralToken:
return Number((initializer as ts.NumericLiteral).text);
Expand Down
29 changes: 29 additions & 0 deletions tests/fixtures/controllers/exampleController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface Location {
city: string;
}

export interface Doc {
id: number;
description: string;
}

@Route('ExampleTest')
export class ExampleTestController {
/**
Expand Down Expand Up @@ -222,4 +227,28 @@ export class ExampleTestController {
public async responseMultiExamplesWithProduces(): Promise<string> {
return 'test 1';
}

@Example<Doc>({
id: -1,
description: 'test doc des',
})
@Get('ResponseExampleWithMinusOperatorPrefixValue')
public async responseExampleWithMinusOperatorPrefixValue(): Promise<Doc> {
return {
id: -1,
description: 'test doc des',
};
}

@Example<Doc>({
id: +1,
description: 'test doc des',
})
@Get('ResponseExampleWithPlusOperatorPrefixValue')
public async responseExampleWithPlusOperatorPrefixValue(): Promise<Doc> {
return {
id: 1,
description: 'test doc des',
};
}
}
22 changes: 22 additions & 0 deletions tests/unit/swagger/schemaDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,28 @@ describe('Schema details generation', () => {

expect(examples).to.deep.eq('test example response');
});

it('uses minus prefix token number value at @Example model', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate();
const exampleSpec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();
const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithMinusOperatorPrefixValue']?.get?.responses?.[200]?.examples?.['application/json'];

expect(examples).to.deep.eq({
id: -1,
description: 'test doc des',
});
});

it('uses plus prefix token number value at @Example model', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate();
const exampleSpec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec();
const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithPlusOperatorPrefixValue']?.get?.responses?.[200]?.examples?.['application/json'];

expect(examples).to.deep.eq({
id: 1,
description: 'test doc des',
});
});
});
});
});
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,36 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
},
});
});

it('uses minus prefix token number value at @Example model', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate();
const exampleSpec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();
const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithMinusOperatorPrefixValue']?.get?.responses?.[200]?.content?.['application/json'].examples;

expect(examples).to.deep.eq({
'Example 1': {
value: {
id: -1,
description: 'test doc des',
},
},
});
});

it('uses plus prefix token number value at @Example model', () => {
const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate();
const exampleSpec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec();
const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithPlusOperatorPrefixValue']?.get?.responses?.[200]?.content?.['application/json'].examples;

expect(examples).to.deep.eq({
'Example 1': {
value: {
id: 1,
description: 'test doc des',
},
},
});
});
});

describe('deprecation', () => {
Expand Down