Skip to content

Commit aea5360

Browse files
[bugfix][kotlin-wiremock] fix problems with range responses (OpenAPITools#19309)
* [kotlin] Target correct library in jvm-spring-webclient sample * [kotlin] Fixed warning in jvm-spring-restclient * [kotlin-wiremock] fixed issue 7193 * [kotlin-wiremock] fixed wrong implementation of fromResponse * [kotlin-wiremock] forbidden API * [kotlin-wiremock] fixed wrong workflow trigger
1 parent 908edde commit aea5360

File tree

28 files changed

+882
-124
lines changed

28 files changed

+882
-124
lines changed

.github/workflows/samples-kotlin-wiremock.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ name: Samples Kotlin WireMock
33
on:
44
push:
55
branches:
6-
- samples/server/petstore/kotlin-wiremock/**
6+
- samples/server/petstore/kotlin-wiremock*/**
77
- samples/server/echo_api/kotlin-wiremock/**
88
pull_request:
99
paths:
10-
- samples/server/petstore/kotlin-wiremock/**
10+
- samples/server/petstore/kotlin-wiremock*/**
1111
- samples/server/echo_api/kotlin-wiremock/**
1212

1313
jobs:
@@ -19,6 +19,7 @@ jobs:
1919
matrix:
2020
sample:
2121
- samples/server/petstore/kotlin-wiremock
22+
- samples/server/petstore/kotlin-wiremock-responses
2223
- samples/server/echo_api/kotlin-wiremock
2324
steps:
2425
- uses: actions/checkout@v4
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generatorName: kotlin-wiremock
2+
outputDir: samples/server/petstore/kotlin-wiremock-responses
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue7193-responses.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/kotlin-wiremock

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.openapitools.codegen.languages;
22

3-
import org.openapitools.codegen.CodegenConstants;
4-
import org.openapitools.codegen.CodegenProperty;
5-
import org.openapitools.codegen.CodegenType;
6-
import org.openapitools.codegen.SupportingFile;
3+
import io.swagger.v3.oas.models.responses.ApiResponse;
4+
import org.openapitools.codegen.*;
75
import org.openapitools.codegen.meta.GeneratorMetadata;
86
import org.openapitools.codegen.meta.Stability;
97
import org.openapitools.codegen.meta.features.GlobalFeature;
@@ -16,13 +14,16 @@
1614
import java.io.File;
1715
import java.util.EnumSet;
1816
import java.util.List;
17+
import java.util.Locale;
1918
import java.util.stream.Collectors;
2019
import java.util.stream.Stream;
2120

2221
public class KotlinWiremockServerCodegen extends AbstractKotlinCodegen {
2322

2423
protected static final String VENDOR_EXTENSION_BASE_NAME_LITERAL = "x-base-name-literal";
2524

25+
protected static final String VENDOR_EXTENSION_IS_RANGE_RESPONSE_CODE = "x-is-range-code";
26+
2627
@Override
2728
public CodegenType getTag() {
2829
return CodegenType.SERVER;
@@ -126,6 +127,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
126127
return objects;
127128
}
128129

130+
@Override
131+
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
132+
var r = super.fromResponse(responseCode, response);
133+
134+
var isRange = List.of("1xx", "2xx", "3xx", "4xx", "5xx").contains(responseCode.toLowerCase(Locale.ROOT));
135+
r.vendorExtensions.put(VENDOR_EXTENSION_IS_RANGE_RESPONSE_CODE, isRange);
136+
return r;
137+
}
138+
129139
@Override
130140
public void postProcess() {
131141
System.out.println("################################################################################");

modules/openapi-generator/src/main/resources/kotlin-wiremock/api-stub-builder.mustache

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,68 @@ import {{modelPackage}}.*
2121
class {{operationIdCamelCase}}StubBuilder internal constructor(private val objectMapper: ObjectMapper, private val stub: MappingBuilder) {
2222
{{#responses}}
2323
{{^wildcard}}
24+
{{^vendorExtensions.x-is-range-code}}
2425

2526
/**
2627
* Let the stub for {{operationId}} respond with HTTP status code {{code}}.
2728
*
28-
{{#returnType}}
29+
{{#dataType}}
2930
* @param body response body for the [MappingBuilder].
30-
{{/returnType}}
31-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
31+
{{/dataType}}
32+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
3233
* @return a [MappingBuilder] to be registered with a WireMock instance.
3334
*/
3435
fun respondWith{{code}}(
35-
{{#returnType}}
36+
{{#dataType}}
3637
body: {{{.}}},
37-
{{/returnType}}
38+
{{/dataType}}
3839
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this },
3940
): MappingBuilder =
4041
stub.willReturn(aResponse()
4142
.withStatus({{code}})
42-
{{#returnType}}
43+
{{#dataType}}
4344
.withHeader("Content-Type", "application/json")
4445
.withBody(objectMapper.writeValueAsString(body))
45-
{{/returnType}}
46+
{{/dataType}}
4647
.configurer()
4748
)
49+
{{/vendorExtensions.x-is-range-code}}
4850
{{/wildcard}}
51+
{{#vendorExtensions.x-is-range-code}}
52+
53+
/**
54+
* Let the stub for {{operationId}} respond with HTTP status code [code].
55+
*
56+
* @param code the response code.
57+
{{#dataType}}
58+
* @param body response body for the [MappingBuilder].
59+
{{/dataType}}
60+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
61+
* @return a [MappingBuilder] to be registered with a WireMock instance.
62+
*/
63+
fun respondWith{{code}}(
64+
code: Int,
65+
{{#dataType}}
66+
body: {{{.}}},
67+
{{/dataType}}
68+
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this },
69+
): MappingBuilder =
70+
stub.willReturn(aResponse()
71+
.withStatus(code)
72+
{{#dataType}}
73+
.withHeader("Content-Type", "application/json")
74+
.withBody(objectMapper.writeValueAsString(body))
75+
{{/dataType}}
76+
.configurer()
77+
)
78+
{{/vendorExtensions.x-is-range-code}}
4979
{{/responses}}
5080

5181
/**
5282
* Let the stub for {{operationId}} respond with HTTP status code [code].
5383
*
54-
{{#returnType}}
84+
* @param code the response code.
5585
* @param body response body for the [MappingBuilder].
56-
{{/returnType}}
5786
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
5887
* @return a [MappingBuilder] to be registered with a WireMock instance.
5988
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
openapi: 3.0.0
2+
info:
3+
description: >-
4+
This spec is mainly for testing Petstore server and contains fake endpoints,
5+
models. Please do not use this for any other purpose. Special characters: "
6+
\
7+
version: 1.0.0
8+
title: OpenAPI Petstore
9+
license:
10+
name: Apache-2.0
11+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
12+
paths:
13+
/foo:
14+
get:
15+
responses:
16+
default:
17+
description: response
18+
content:
19+
application/json:
20+
schema:
21+
type: number
22+
4XX:
23+
description: client error
24+
content:
25+
application/json:
26+
schema:
27+
$ref: '#/components/schemas/Foo'
28+
404:
29+
description: not found
30+
content:
31+
application/json:
32+
schema:
33+
type: string
34+
400:
35+
description: bad request
36+
content:
37+
application/json:
38+
schema:
39+
type: array
40+
items:
41+
$ref: '#/components/schemas/Foo'
42+
components:
43+
schemas:
44+
Foo:
45+
type: object
46+
properties:
47+
bar:
48+
$ref: '#/components/schemas/Bar'
49+
Bar:
50+
type: string
51+
default: bar

samples/server/echo_api/kotlin-wiremock/src/main/kotlin/org/openapitools/apis/AuthApiStubBuilders.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestAuthHttpBasicStubBuilder internal constructor(private val objectMapper
2222
* Let the stub for testAuthHttpBasic respond with HTTP status code 200.
2323
*
2424
* @param body response body for the [MappingBuilder].
25-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
25+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
2626
* @return a [MappingBuilder] to be registered with a WireMock instance.
2727
*/
2828
fun respondWith200(
@@ -39,6 +39,7 @@ class TestAuthHttpBasicStubBuilder internal constructor(private val objectMapper
3939
/**
4040
* Let the stub for testAuthHttpBasic respond with HTTP status code [code].
4141
*
42+
* @param code the response code.
4243
* @param body response body for the [MappingBuilder].
4344
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
4445
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -69,7 +70,7 @@ class TestAuthHttpBearerStubBuilder internal constructor(private val objectMappe
6970
* Let the stub for testAuthHttpBearer respond with HTTP status code 200.
7071
*
7172
* @param body response body for the [MappingBuilder].
72-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
73+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
7374
* @return a [MappingBuilder] to be registered with a WireMock instance.
7475
*/
7576
fun respondWith200(
@@ -86,6 +87,7 @@ class TestAuthHttpBearerStubBuilder internal constructor(private val objectMappe
8687
/**
8788
* Let the stub for testAuthHttpBearer respond with HTTP status code [code].
8889
*
90+
* @param code the response code.
8991
* @param body response body for the [MappingBuilder].
9092
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
9193
* @return a [MappingBuilder] to be registered with a WireMock instance.

samples/server/echo_api/kotlin-wiremock/src/main/kotlin/org/openapitools/apis/BodyApiStubBuilders.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestBinaryGifStubBuilder internal constructor(private val objectMapper: Ob
2222
* Let the stub for testBinaryGif respond with HTTP status code 200.
2323
*
2424
* @param body response body for the [MappingBuilder].
25-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
25+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
2626
* @return a [MappingBuilder] to be registered with a WireMock instance.
2727
*/
2828
fun respondWith200(
@@ -39,6 +39,7 @@ class TestBinaryGifStubBuilder internal constructor(private val objectMapper: Ob
3939
/**
4040
* Let the stub for testBinaryGif respond with HTTP status code [code].
4141
*
42+
* @param code the response code.
4243
* @param body response body for the [MappingBuilder].
4344
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
4445
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -69,7 +70,7 @@ class TestBodyApplicationOctetstreamBinaryStubBuilder internal constructor(priva
6970
* Let the stub for testBodyApplicationOctetstreamBinary respond with HTTP status code 200.
7071
*
7172
* @param body response body for the [MappingBuilder].
72-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
73+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
7374
* @return a [MappingBuilder] to be registered with a WireMock instance.
7475
*/
7576
fun respondWith200(
@@ -86,6 +87,7 @@ class TestBodyApplicationOctetstreamBinaryStubBuilder internal constructor(priva
8687
/**
8788
* Let the stub for testBodyApplicationOctetstreamBinary respond with HTTP status code [code].
8889
*
90+
* @param code the response code.
8991
* @param body response body for the [MappingBuilder].
9092
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
9193
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -116,7 +118,7 @@ class TestBodyMultipartFormdataArrayOfBinaryStubBuilder internal constructor(pri
116118
* Let the stub for testBodyMultipartFormdataArrayOfBinary respond with HTTP status code 200.
117119
*
118120
* @param body response body for the [MappingBuilder].
119-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
121+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
120122
* @return a [MappingBuilder] to be registered with a WireMock instance.
121123
*/
122124
fun respondWith200(
@@ -133,6 +135,7 @@ class TestBodyMultipartFormdataArrayOfBinaryStubBuilder internal constructor(pri
133135
/**
134136
* Let the stub for testBodyMultipartFormdataArrayOfBinary respond with HTTP status code [code].
135137
*
138+
* @param code the response code.
136139
* @param body response body for the [MappingBuilder].
137140
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
138141
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -163,7 +166,7 @@ class TestBodyMultipartFormdataSingleBinaryStubBuilder internal constructor(priv
163166
* Let the stub for testBodyMultipartFormdataSingleBinary respond with HTTP status code 200.
164167
*
165168
* @param body response body for the [MappingBuilder].
166-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
169+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
167170
* @return a [MappingBuilder] to be registered with a WireMock instance.
168171
*/
169172
fun respondWith200(
@@ -180,6 +183,7 @@ class TestBodyMultipartFormdataSingleBinaryStubBuilder internal constructor(priv
180183
/**
181184
* Let the stub for testBodyMultipartFormdataSingleBinary respond with HTTP status code [code].
182185
*
186+
* @param code the response code.
183187
* @param body response body for the [MappingBuilder].
184188
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
185189
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -210,7 +214,7 @@ class TestEchoBodyFreeFormObjectResponseStringStubBuilder internal constructor(p
210214
* Let the stub for testEchoBodyFreeFormObjectResponseString respond with HTTP status code 200.
211215
*
212216
* @param body response body for the [MappingBuilder].
213-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
217+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
214218
* @return a [MappingBuilder] to be registered with a WireMock instance.
215219
*/
216220
fun respondWith200(
@@ -227,6 +231,7 @@ class TestEchoBodyFreeFormObjectResponseStringStubBuilder internal constructor(p
227231
/**
228232
* Let the stub for testEchoBodyFreeFormObjectResponseString respond with HTTP status code [code].
229233
*
234+
* @param code the response code.
230235
* @param body response body for the [MappingBuilder].
231236
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
232237
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -257,7 +262,7 @@ class TestEchoBodyPetStubBuilder internal constructor(private val objectMapper:
257262
* Let the stub for testEchoBodyPet respond with HTTP status code 200.
258263
*
259264
* @param body response body for the [MappingBuilder].
260-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
265+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
261266
* @return a [MappingBuilder] to be registered with a WireMock instance.
262267
*/
263268
fun respondWith200(
@@ -274,6 +279,7 @@ class TestEchoBodyPetStubBuilder internal constructor(private val objectMapper:
274279
/**
275280
* Let the stub for testEchoBodyPet respond with HTTP status code [code].
276281
*
282+
* @param code the response code.
277283
* @param body response body for the [MappingBuilder].
278284
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
279285
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -304,7 +310,7 @@ class TestEchoBodyPetResponseStringStubBuilder internal constructor(private val
304310
* Let the stub for testEchoBodyPetResponseString respond with HTTP status code 200.
305311
*
306312
* @param body response body for the [MappingBuilder].
307-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
313+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
308314
* @return a [MappingBuilder] to be registered with a WireMock instance.
309315
*/
310316
fun respondWith200(
@@ -321,6 +327,7 @@ class TestEchoBodyPetResponseStringStubBuilder internal constructor(private val
321327
/**
322328
* Let the stub for testEchoBodyPetResponseString respond with HTTP status code [code].
323329
*
330+
* @param code the response code.
324331
* @param body response body for the [MappingBuilder].
325332
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
326333
* @return a [MappingBuilder] to be registered with a WireMock instance.
@@ -351,7 +358,7 @@ class TestEchoBodyTagResponseStringStubBuilder internal constructor(private val
351358
* Let the stub for testEchoBodyTagResponseString respond with HTTP status code 200.
352359
*
353360
* @param body response body for the [MappingBuilder].
354-
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
361+
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
355362
* @return a [MappingBuilder] to be registered with a WireMock instance.
356363
*/
357364
fun respondWith200(
@@ -368,6 +375,7 @@ class TestEchoBodyTagResponseStringStubBuilder internal constructor(private val
368375
/**
369376
* Let the stub for testEchoBodyTagResponseString respond with HTTP status code [code].
370377
*
378+
* @param code the response code.
371379
* @param body response body for the [MappingBuilder].
372380
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
373381
* @return a [MappingBuilder] to be registered with a WireMock instance.

0 commit comments

Comments
 (0)