Skip to content

Commit 7d4200b

Browse files
committed
improved response content grouping
1 parent 57f461e commit 7d4200b

5 files changed

Lines changed: 99 additions & 16 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/model/Endpoint.groovy

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class Endpoint {
9494
* @return list of method responses
9595
*/
9696
List<EndpointResponse> getEndpointResponses () {
97-
List<Response> oks = successResponses
98-
List<Response> errors = errorResponses
97+
Set<Response> oks = successResponses
98+
Set<Response> errors = errorResponses
9999
oks.collect {
100100
new EndpointResponse(main: it, errors: errors)
101101
}
@@ -104,29 +104,29 @@ class Endpoint {
104104
/**
105105
* finds the success responses
106106
*/
107-
private List<Response> getSuccessResponses () {
108-
def success = responses.keySet ()
109-
.findAll {
110-
it.startsWith ('2')
111-
}
107+
private Set<Response> getSuccessResponses () {
108+
Map<String, Response> result = [:]
112109

113-
if (success.size () == 1) {
114-
return responses."${success.first ()}"
110+
responses.findAll {
111+
it.key.startsWith ('2')
112+
}.each {
113+
it.value.each {
114+
result.put (it.contentType, it)
115+
}
115116
}
116117

117-
println "Endpoint: can't find successful responses (${path}/${success})"
118-
[]
118+
result.values () as Set<Response>
119119
}
120120

121121
/**
122122
* finds the error responses
123123
*/
124-
private List<Response> getErrorResponses () {
124+
private Set<Response> getErrorResponses () {
125125
responses.findAll {
126126
!it.key.startsWith ('2')
127127
}.collect {
128128
it.value.first ()
129-
}
129+
} as Set<Response>
130130
}
131131

132132
}

src/testInt/groovy/com/github/hauner/openapi/processor/ProcessorEndToEndTest.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class ProcessorEndToEndTest extends ProcessorTestBase {
4545
'response-array-data-type-mapping',
4646
'response-complex-data-types',
4747
'response-content-multiple',
48-
'response-simple-data-types'
48+
'response-content-single',
49+
'response-simple-data-types',
50+
'schema-composed'
4951
]
5052

5153
@Parameterized.Parameters(name = "{0}")

src/testInt/groovy/com/github/hauner/openapi/processor/ProcessorPendingTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class ProcessorPendingTest extends ProcessorTestBase {
2828
@Parameterized.Parameters(name = "{0}")
2929
static Collection<TestSet> sources () {
3030
return [
31-
new TestSet(name: 'schema-composed', parser: ParserType.SWAGGER),
32-
new TestSet(name: 'schema-composed', parser: ParserType.OPENAPI4J)
31+
new TestSet(name: 'response-content-single', parser: ParserType.SWAGGER),
32+
new TestSet(name: 'response-content-single', parser: ParserType.OPENAPI4J)
3333
]
3434
}
3535

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-spring.
3+
* DO NOT EDIT.
4+
*/
5+
6+
package generated.api;
7+
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
11+
public interface Api {
12+
13+
@GetMapping(path = "/foo")
14+
ResponseEntity<Void> getFoo();
15+
16+
@GetMapping(
17+
path = "/bar",
18+
produces = {"text/plain"})
19+
ResponseEntity<String> getBar();
20+
21+
@GetMapping(
22+
path = "/bar-multi",
23+
produces = {"text/plain"})
24+
ResponseEntity<String> getBarMultiTextPlain();
25+
26+
@GetMapping(
27+
path = "/bar-multi",
28+
produces = {"application/json"})
29+
ResponseEntity<String> getBarMultiApplicationJson();
30+
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.0.2
2+
info:
3+
title: test multiple 2xx responses with same content
4+
version: 1.0.0
5+
6+
paths:
7+
/foo:
8+
get:
9+
summary: same content (no content)
10+
responses:
11+
'202':
12+
description: accepted
13+
'201':
14+
description: created
15+
16+
/bar:
17+
get:
18+
summary: same content
19+
responses:
20+
'202':
21+
description: accepted
22+
content:
23+
text/plain:
24+
schema:
25+
type: string
26+
27+
'201':
28+
description: created
29+
content:
30+
text/plain:
31+
schema:
32+
type: string
33+
34+
/bar-multi:
35+
get:
36+
summary: same content
37+
responses:
38+
'202':
39+
description: accepted
40+
content:
41+
text/plain:
42+
schema:
43+
type: string
44+
45+
'201':
46+
description: created
47+
content:
48+
application/json:
49+
schema:
50+
type: string

0 commit comments

Comments
 (0)