Skip to content

Commit 4ca8a39

Browse files
committed
Fixing NPEs when schemas are referenced and not directly included in the specification of a sse endpoint. Fixes #17271
1 parent baf0175 commit 4ca8a39

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
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
@@ -70,6 +70,7 @@
7070
import org.openapitools.codegen.templating.mustache.SplitStringLambda;
7171
import org.openapitools.codegen.templating.mustache.SpringHttpStatusLambda;
7272
import org.openapitools.codegen.templating.mustache.TrimWhitespaceLambda;
73+
import org.openapitools.codegen.utils.ModelUtils;
7374
import org.openapitools.codegen.utils.ProcessUtils;
7475
import org.openapitools.codegen.utils.URLPathUtils;
7576
import org.slf4j.Logger;
@@ -1044,6 +1045,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10441045
.filter(p -> p.getRight().is2xx) // consider only success
10451046
.map(p -> p.getLeft().getContent().get(MEDIA_EVENT_STREAM))
10461047
.map(MediaType::getSchema)
1048+
.map(s -> ModelUtils.unaliasSchema(openAPI, s))
10471049
.collect(Collectors.toList()).stream()
10481050
.collect(Collectors.groupingBy(Schema::getType));
10491051
if(schemaTypes.containsKey("array")) {

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,17 +4496,15 @@ public void testSSEOperationSupport() throws Exception {
44964496
.assertMethod("sse2", "ServerWebExchange")
44974497
.hasReturnType("Flux<EventType>")
44984498
.toFileAssert()
4499-
// FIXME: Generator Crash
4500-
// .assertMethod("sse3", "ServerWebExchange")
4501-
// .hasReturnType("Flux<String>")
4502-
// .toFileAssert()
4499+
.assertMethod("sse3", "ServerWebExchange")
4500+
.hasReturnType("Flux<String>")
4501+
.toFileAssert()
45034502
.assertMethod("sse4", "ServerWebExchange")
45044503
.hasReturnType("Flux<String>")
45054504
.toFileAssert()
4506-
// FIXME: Generator Crash, see #17271
4507-
// .assertMethod("sse5", "ServerWebExchange")
4508-
// .hasReturnType("Flux<EventType>")
4509-
// .toFileAssert()
4505+
.assertMethod("sse5", "ServerWebExchange")
4506+
.hasReturnType("Flux<EventType>")
4507+
.toFileAssert()
45104508

45114509
// FIXME: Generator Crash
45124510
// .assertMethod("notSse1", "ServerWebExchange")
@@ -4539,20 +4537,18 @@ public void testSSEOperationSupport() throws Exception {
45394537
.hasReturnType("Flux<EventType>")
45404538
.bodyContainsLines("return Flux.empty();")
45414539
.toFileAssert()
4542-
// FIXME: Generator Crash
4543-
// .assertMethod("sse3", "ServerWebExchange")
4544-
// .hasReturnType("Flux<String>")
4545-
// .bodyContainsLines("return Flux.empty();")
4546-
// .toFileAssert()
4540+
.assertMethod("sse3", "ServerWebExchange")
4541+
.hasReturnType("Flux<String>")
4542+
.bodyContainsLines("return Flux.empty();")
4543+
.toFileAssert()
45474544
.assertMethod("sse4", "ServerWebExchange")
45484545
.hasReturnType("Flux<String>")
45494546
.bodyContainsLines("return Flux.empty();")
45504547
.toFileAssert()
4551-
// FIXME: Generator Crash
4552-
// .assertMethod("sse5", "ServerWebExchange")
4553-
// .hasReturnType("Flux<String>")
4554-
// .bodyContainsLines("return Flux.empty();")
4555-
// .toFileAssert()
4548+
.assertMethod("sse5", "ServerWebExchange")
4549+
.hasReturnType("Flux<EventType>")
4550+
.bodyContainsLines("return Flux.empty();")
4551+
.toFileAssert()
45564552

45574553
// FIXME: Generator Crash
45584554
// .assertMethod("notSse1", "ServerWebExchange")

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,29 @@ paths:
3434
format: event-stream
3535
items:
3636
$ref: "#/components/schemas/EventType"
37-
# FIXME: Generator Crash
38-
# /path/sse/3:
39-
# get:
40-
# operationId: sse_3
41-
# responses:
42-
# "200":
43-
# description: using reference for schema
44-
# content:
45-
# text/event-stream:
46-
# schema:
47-
# $ref: "#/components/schemas/EventTypeStream"
37+
/path/sse/3:
38+
get:
39+
operationId: sse_3
40+
responses:
41+
"200":
42+
description: using reference for schema
43+
content:
44+
text/event-stream:
45+
schema:
46+
$ref: "#/components/schemas/Stream"
4847
/path/sse/4:
4948
get:
5049
operationId: sse_4
5150
responses:
5251
"200":
53-
$ref: "#/components/responses/EventTypeResponse"
54-
# FIXME: generator crash
55-
# /path/sse/5:
56-
# post:
57-
# operationId: sse_5
58-
# description: indirect references, see Issue 17271
59-
# responses:
60-
# '200':
61-
# $ref: '#/components/responses/EventTypeResponseWithReference'
52+
$ref: "#/components/responses/StreamResponse"
53+
/path/sse/5:
54+
post:
55+
operationId: sse_5
56+
description: indirect references, see Issue 17271
57+
responses:
58+
'200':
59+
$ref: '#/components/responses/StreamResponseWithReference'
6260

6361
# FIXME: generator crash
6462
# /path/notsse/1:
@@ -132,6 +130,11 @@ components:
132130
attribute1:
133131
type: string
134132
EventTypeStream:
133+
type: array
134+
format: event-stream
135+
items:
136+
$ref: '#/components/schemas/EventType'
137+
Stream:
135138
type: array
136139
format: event-stream
137140
items:
@@ -142,7 +145,7 @@ components:
142145

143146

144147
responses:
145-
EventTypeResponse:
148+
StreamResponse:
146149
description: response
147150
content:
148151
text/event-stream:
@@ -152,8 +155,8 @@ components:
152155
items:
153156
type: string
154157

155-
EventTypeResponseWithReference:
156-
description: acknowledged
158+
StreamResponseWithReference:
159+
description: response
157160
content:
158161
text/event-stream:
159162
schema:

0 commit comments

Comments
 (0)