Skip to content

Commit 7ca3fc3

Browse files
hertzsprungJames Shaw
andauthored
Fix response model generation with ParseOptions.resolveResponses=true (#21568)
* set resolveResponses=true needed by swagger-parser>=2.1.23, see swagger-api/swagger-parser#2127 * update samples --------- Co-authored-by: James Shaw <[email protected]>
1 parent f632ab7 commit 7ca3fc3

File tree

7 files changed

+72
-6
lines changed

7 files changed

+72
-6
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ public Context<?> toContext() {
683683
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
684684
ParseOptions options = new ParseOptions();
685685
options.setResolve(true);
686+
options.setResolveResponses(true);
686687
SwaggerParseResult result = new OpenAPIParser().readLocation(inputSpec, authorizationValues, options);
687688

688689
// TODO: Move custom validations to a separate type as part of a "Workflow"

modules/openapi-generator/src/test/java/org/openapitools/codegen/config/CodegenConfiguratorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.openapitools.codegen.config;
1818

19+
import io.swagger.v3.oas.models.OpenAPI;
20+
import org.junit.jupiter.api.Assertions;
1921
import org.openapitools.codegen.ClientOptInput;
2022
import org.openapitools.codegen.CodegenConfig;
2123
import org.openapitools.codegen.CodegenConstants;
@@ -121,4 +123,14 @@ public void shouldSetConfigProperties() throws IOException {
121123
want(props, "foo", "bar");
122124
want(props, "baz", "quux");
123125
}
126+
127+
@Test
128+
public void resolvesResponses() {
129+
@SuppressWarnings("unchecked") Context<OpenAPI> context = (Context<OpenAPI>) new CodegenConfigurator()
130+
.setInputSpec("src/test/resources/3_0/response-ref.yaml")
131+
.setGeneratorName("java")
132+
.toContext();
133+
134+
Assertions.assertNotNull(context.getSpecDocument().getPaths().get("/hello").getGet().getResponses().get("200").getContent());
135+
}
124136
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.1
2+
info:
3+
title: ping test
4+
version: '1.0'
5+
servers:
6+
- url: 'http://localhost:8000/'
7+
paths:
8+
/hello:
9+
get:
10+
operationId: hello
11+
responses:
12+
'200':
13+
$ref: "#/components/responses/refResponse"
14+
15+
components:
16+
responses:
17+
refResponse:
18+
description: a response specification `$ref`erenced from an operation
19+
content:
20+
application/json:
21+
schema:
22+
type: object
23+
properties:
24+
responseProperty:
25+
type: string

samples/openapi3/server/petstore/spring-boot-oneof-sealed/src/main/resources/openapi.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ paths:
1515
operationId: getAllFoos
1616
responses:
1717
"200":
18-
$ref: "#/components/responses/200FooArray"
18+
content:
19+
application/json;charset=utf-8:
20+
schema:
21+
items:
22+
$ref: "#/components/schemas/FooRefOrValue"
23+
type: array
24+
description: Success
1925
summary: GET all Foos
2026
tags:
2127
- Foo
@@ -29,7 +35,11 @@ paths:
2935
$ref: "#/components/requestBodies/Foo"
3036
responses:
3137
"201":
32-
$ref: "#/components/responses/201Foo"
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/FooRefOrValue"
42+
description: Error
3343
summary: Create a Foo
3444
tags:
3545
- Foo

samples/openapi3/server/petstore/spring-boot-oneof/src/main/resources/openapi.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ paths:
1515
operationId: getAllFoos
1616
responses:
1717
"200":
18-
$ref: "#/components/responses/200FooArray"
18+
content:
19+
application/json;charset=utf-8:
20+
schema:
21+
items:
22+
$ref: "#/components/schemas/FooRefOrValue"
23+
type: array
24+
description: Success
1925
summary: GET all Foos
2026
tags:
2127
- Foo
@@ -29,7 +35,11 @@ paths:
2935
$ref: "#/components/requestBodies/Foo"
3036
responses:
3137
"201":
32-
$ref: "#/components/responses/201Foo"
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/FooRefOrValue"
42+
description: Error
3343
summary: Create a Foo
3444
tags:
3545
- Foo

samples/server/petstore/go-api-server/api/openapi.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,11 @@ paths:
918918
$ref: "#/components/requestBodies/TestBody"
919919
responses:
920920
"200":
921-
$ref: "#/components/responses/SuccessfulOp"
921+
content:
922+
application/json:
923+
schema:
924+
type: bool
925+
description: Successful Operation
922926
summary: POST a test batch
923927
tags:
924928
- fake

samples/server/petstore/go-chi-server/api/openapi.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,11 @@ paths:
918918
$ref: "#/components/requestBodies/TestBody"
919919
responses:
920920
"200":
921-
$ref: "#/components/responses/SuccessfulOp"
921+
content:
922+
application/json:
923+
schema:
924+
type: bool
925+
description: Successful Operation
922926
summary: POST a test batch
923927
tags:
924928
- fake

0 commit comments

Comments
 (0)