Skip to content

Commit 3c5967f

Browse files
author
Kathrin Geilmann
committed
Fixing NPEs when the response does not contain a text/event-stream entry. Fixing NPE when the response does not contain any schema
1 parent 82e9d4b commit 3c5967f

File tree

3 files changed

+58
-64
lines changed

3 files changed

+58
-64
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,11 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10411041
$ref: <typeRef>
10421042
*/
10431043
Map<String, List<Schema>> schemaTypes = operation.getResponses().entrySet().stream()
1044+
.filter(p -> p.getValue().getContent() != null || p.getValue().get$ref() != null)
10441045
.map(e -> Pair.of(e.getValue(), fromResponse(e.getKey(), e.getValue())))
10451046
.filter(p -> p.getRight().is2xx) // consider only success
10461047
.map(p -> p.getLeft().getContent().get(MEDIA_EVENT_STREAM))
1048+
.filter(Objects::nonNull)
10471049
.map(MediaType::getSchema)
10481050
.map(s -> ModelUtils.unaliasSchema(openAPI, s))
10491051
.collect(Collectors.toList()).stream()

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,6 +4489,7 @@ public void testSSEOperationSupport() throws Exception {
44894489
File api = files.get("PathApi.java");
44904490
File delegate = files.get("PathApiDelegate.java");
44914491

4492+
// SSE Endpoints
44924493
JavaFileAssert.assertThat(api)
44934494
.assertMethod("sse1", "ServerWebExchange")
44944495
.hasReturnType("Flux<String>")
@@ -4503,17 +4504,15 @@ public void testSSEOperationSupport() throws Exception {
45034504
.hasReturnType("Flux<String>")
45044505
.toFileAssert()
45054506
.assertMethod("sse5", "ServerWebExchange")
4506-
.hasReturnType("Flux<EventType>")
4507+
.hasReturnType("Flux<EventType>");
4508+
//Non SSE Endpoints
4509+
JavaFileAssert.assertThat(api)
4510+
.assertMethod("notSse1", "ServerWebExchange")
4511+
.hasReturnType("Mono<ResponseEntity<Void>>")
4512+
.toFileAssert()
4513+
.assertMethod("notSse2", "ServerWebExchange")
4514+
.hasReturnType("Mono<ResponseEntity<Flux<String>>>")
45074515
.toFileAssert()
4508-
4509-
// FIXME: Generator Crash
4510-
// .assertMethod("notSse1", "ServerWebExchange")
4511-
// .hasReturnType("Mono<ResponseEntity<Void>>")
4512-
// .toFileAssert()
4513-
// FIXME: Generator Crash
4514-
// .assertMethod("notSse2", "ServerWebExchange")
4515-
// .hasReturnType("Mono<ResponseEntity<Flux<String>>>")
4516-
// .toFileAssert()
45174516
.assertMethod("notSse3", "ServerWebExchange")
45184517
.hasReturnType("Mono<ResponseEntity<String>>")
45194518
.toFileAssert()
@@ -4523,11 +4522,10 @@ public void testSSEOperationSupport() throws Exception {
45234522
.assertMethod("notSse7", "ServerWebExchange")
45244523
.hasReturnType("Mono<ResponseEntity<String>>")
45254524
.toFileAssert()
4526-
// FIXME: Generator Crash
4527-
// .assertMethod("notSse8", "ServerWebExchange")
4528-
// .hasReturnType("Mono<ResponseEntity<String>>")
4529-
;
4525+
.assertMethod("notSse8", "ServerWebExchange")
4526+
.hasReturnType("Mono<ResponseEntity<String>>");
45304527

4528+
// SSE Endpoints
45314529
JavaFileAssert.assertThat(delegate)
45324530
.assertMethod("sse1", "ServerWebExchange")
45334531
.hasReturnType("Flux<String>")
@@ -4547,19 +4545,18 @@ public void testSSEOperationSupport() throws Exception {
45474545
.toFileAssert()
45484546
.assertMethod("sse5", "ServerWebExchange")
45494547
.hasReturnType("Flux<EventType>")
4550-
.bodyContainsLines("return Flux.empty();")
4551-
.toFileAssert()
4548+
.bodyContainsLines("return Flux.empty();");
45524549

4553-
// FIXME: Generator Crash
4554-
// .assertMethod("notSse1", "ServerWebExchange")
4555-
// .hasReturnType("Mono<ResponseEntity<Void>>")
4556-
// .bodyContainsLines("return result.then(Mono.empty());")
4557-
// .toFileAssert()
4558-
// FIXME: Generator Crash
4559-
// .assertMethod("notSse2", "ServerWebExchange")
4560-
// .hasReturnType("Mono<ResponseEntity<Flux<String>>>")
4561-
// .bodyContainsLines("return result.then(Mono.empty());")
4562-
// .toFileAssert()
4550+
// Non SSE Endpoints
4551+
JavaFileAssert.assertThat(delegate)
4552+
.assertMethod("notSse1", "ServerWebExchange")
4553+
.hasReturnType("Mono<ResponseEntity<Void>>")
4554+
.bodyContainsLines("return result.then(Mono.empty());")
4555+
.toFileAssert()
4556+
.assertMethod("notSse2", "ServerWebExchange")
4557+
.hasReturnType("Mono<ResponseEntity<Flux<String>>>")
4558+
.bodyContainsLines("return result.then(Mono.empty());")
4559+
.toFileAssert()
45634560
.assertMethod("notSse3", "ServerWebExchange")
45644561
.hasReturnType("Mono<ResponseEntity<String>>")
45654562
.bodyContainsLines("return result.then(Mono.empty());")
@@ -4572,12 +4569,9 @@ public void testSSEOperationSupport() throws Exception {
45724569
.hasReturnType("Mono<ResponseEntity<String>>")
45734570
.bodyContainsLines("return result.then(Mono.empty());")
45744571
.toFileAssert()
4575-
// FIXME: Generator Crash
4576-
// .assertMethod("notSse8", "ServerWebExchange")
4577-
// .hasReturnType("Mono<ResponseEntity<String>>")
4578-
// .bodyContainsLines("return result.then(Mono.empty());")
4579-
;
4580-
4572+
.assertMethod("notSse8", "ServerWebExchange")
4573+
.hasReturnType("Mono<ResponseEntity<String>>")
4574+
.bodyContainsLines("return result.then(Mono.empty());");
45814575
}
45824576

45834577
@DataProvider(name = "invalid sse endpoints")

modules/openapi-generator/src/test/resources/3_0/sse.yaml

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,26 @@ paths:
5858
'200':
5959
$ref: '#/components/responses/StreamResponseWithReference'
6060

61-
# FIXME: generator crash
62-
# /path/notsse/1:
63-
# get:
64-
# operationId: not_sse_1
65-
# responses:
66-
# '200':
67-
# description: no schema defined, but this is a valid OpenAPI Specification
68-
# FIXME: generator crash
69-
# /path/notsse/2:
70-
# get:
71-
# operationId: not_sse_2
72-
# responses:
73-
# '200':
74-
# description: wrong media type
75-
# content:
76-
# application/json:
77-
# schema:
78-
# type: array
79-
# format: event-stream
80-
# items:
81-
# type: string
61+
62+
/path/notsse/1:
63+
get:
64+
operationId: not_sse_1
65+
responses:
66+
'200':
67+
description: no schema defined, but this is a valid OpenAPI Specification
68+
/path/notsse/2:
69+
get:
70+
operationId: not_sse_2
71+
responses:
72+
'200':
73+
description: wrong media type
74+
content:
75+
application/json:
76+
schema:
77+
type: array
78+
format: event-stream
79+
items:
80+
type: string
8281
/path/notsse/3:
8382
get:
8483
operationId: not_sse_3
@@ -110,17 +109,16 @@ paths:
110109
responses:
111110
'200':
112111
$ref: "#/components/responses/NotAStreamResponse"
113-
# FIXME: Generator Crashed
114-
# /path/notsse/8:
115-
# get:
116-
# operationId: not_sse_8
117-
# responses:
118-
# "200":
119-
# description: referenced schema does not match
120-
# content:
121-
# text/event-stream:
122-
# schema:
123-
# $ref: "#/components/schemas/NotAStreamType"
112+
/path/notsse/8:
113+
get:
114+
operationId: not_sse_8
115+
responses:
116+
"200":
117+
description: referenced schema does not match
118+
content:
119+
text/event-stream:
120+
schema:
121+
$ref: "#/components/schemas/NotAStreamType"
124122

125123
components:
126124
schemas:

0 commit comments

Comments
 (0)