Skip to content

Commit cd3b9a1

Browse files
[typescript-nestjs-server] #22928 add docs, fix indentations and test execution
1 parent 617a8f6 commit cd3b9a1

5 files changed

Lines changed: 66 additions & 11 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNestjsServerCodegen.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ private boolean isLanguagePrimitive(String type) {
278278
return languageSpecificPrimitives.contains(type) || isInlineUnion(type);
279279
}
280280

281+
/**
282+
* <p>
283+
* Determines if the given type is an inline union of strings, described as an enum without being an explicit component in OpenAPI spec.
284+
* </p>
285+
* Example input that matches: {@code "'A' | 'B'" }
286+
*
287+
* @param type The Typescript type to evaluate.
288+
*/
281289
private boolean isInlineUnion(String type) {
282290
return Arrays.stream(type.split("\\|"))
283291
.map(String::trim)
@@ -301,7 +309,7 @@ private boolean isRecordType(String type) {
301309
public void postProcessParameter(CodegenParameter parameter) {
302310
super.postProcessParameter(parameter);
303311
parameter.dataType = applyLocalTypeMapping(parameter.dataType);
304-
if("undefined".equals(parameter.defaultValue)) {
312+
if ("undefined".equals(parameter.defaultValue)) {
305313
parameter.defaultValue = null;
306314
}
307315
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
22

3+
/**
4+
* A decorator function for retrieving cookies from the request object in an HTTP context.
5+
*
6+
* This decorator only works, if the framework specific cookie middleware is installed and enabled.
7+
* - For Express, you need to use the `cookie-parser` middleware.
8+
* - For Fastify, you need to use the `@fastify/cookie` plugin.
9+
*
10+
* Consult https://docs.nestjs.com/techniques/cookies for further information
11+
*
12+
* Usage:
13+
* ```
14+
* @Get()
15+
* findAll(@Cookies('name') name: string) {}
16+
* ```
17+
*/
318
export const Cookies = createParamDecorator((data: string, ctx: ExecutionContext) => {
4-
const request = ctx.switchToHttp().getRequest();
5-
return data ? request.cookies?.[data] : request.cookies;
19+
const request = ctx.switchToHttp().getRequest();
20+
return data ? request.cookies?.[data] : request.cookies;
621
});
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
22

3+
/**
4+
* A decorator function for retrieving cookies from the request object in an HTTP context.
5+
*
6+
* This decorator only works, if the framework specific cookie middleware is installed and enabled.
7+
* - For Express, you need to use the `cookie-parser` middleware.
8+
* - For Fastify, you need to use the `@fastify/cookie` plugin.
9+
*
10+
* Consult https://docs.nestjs.com/techniques/cookies for further information
11+
*
12+
* Usage:
13+
* ```
14+
* @Get()
15+
* findAll(@Cookies('name') name: string) {}
16+
* ```
17+
*/
318
export const Cookies = createParamDecorator((data: string, ctx: ExecutionContext) => {
4-
const request = ctx.switchToHttp().getRequest();
5-
return data ? request.cookies?.[data] : request.cookies;
19+
const request = ctx.switchToHttp().getRequest();
20+
return data ? request.cookies?.[data] : request.cookies;
621
});
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
22

3+
/**
4+
* A decorator function for retrieving cookies from the request object in an HTTP context.
5+
*
6+
* This decorator only works, if the framework specific cookie middleware is installed and enabled.
7+
* - For Express, you need to use the `cookie-parser` middleware.
8+
* - For Fastify, you need to use the `@fastify/cookie` plugin.
9+
*
10+
* Consult https://docs.nestjs.com/techniques/cookies for further information
11+
*
12+
* Usage:
13+
* ```
14+
* @Get()
15+
* findAll(@Cookies('name') name: string) {}
16+
* ```
17+
*/
318
export const Cookies = createParamDecorator((data: string, ctx: ExecutionContext) => {
4-
const request = ctx.switchToHttp().getRequest();
5-
return data ? request.cookies?.[data] : request.cookies;
19+
const request = ctx.switchToHttp().getRequest();
20+
return data ? request.cookies?.[data] : request.cookies;
621
});

samples/server/petstore/typescript-nestjs-server/test/app.e2e-spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ describe('AppModule (e2e)', () => {
5858
it('should set default parameters', () => {
5959
let defaultService: DefaultService = app.get(DefaultApi);
6060

61-
request(app.getHttpServer())
61+
return request(app.getHttpServer())
6262
.get('/test/parameters/path_a/path_b')
63-
.expect(200, () => {
63+
.expect(200)
64+
.then(() => {
6465
expect(defaultService.lastRequestParams).toEqual({
6566
pathDefault: 'path_a',
6667
pathNullable: 'path_b',
@@ -84,7 +85,7 @@ describe('AppModule (e2e)', () => {
8485
it('should receive request parameters', () => {
8586
let defaultService: DefaultService = app.get(DefaultApi);
8687

87-
request(app.getHttpServer())
88+
return request(app.getHttpServer())
8889
.get('/test/parameters/path_a/path_b')
8990
.query({
9091
query_default: 'custom_query',
@@ -101,7 +102,8 @@ describe('AppModule (e2e)', () => {
101102
'Cookie',
102103
'cookie_default=custom_cookie; cookie_default_enum=C; cookie_default_int=7; cookie_nullable=a_cookie',
103104
)
104-
.expect(200, () => {
105+
.expect(200)
106+
.then(() => {
105107
expect(defaultService.lastRequestParams).toEqual({
106108
pathDefault: 'path_a',
107109
pathNullable: 'path_b',

0 commit comments

Comments
 (0)