Skip to content

Commit 552ab4c

Browse files
authored
Java-wiremock: Correctly handle multiple MIME-types in HTTP-Accept-headers (#22133)
* Java-wiremock: Allow matching multiple Accept-header values * Wiremock: Ignore casing when matching requests' Content-Type headers * Java-wiremock: Regenerate samples
1 parent 2b00bab commit 552ab4c

File tree

8 files changed

+104
-80
lines changed

8 files changed

+104
-80
lines changed

modules/openapi-generator/src/main/resources/java-wiremock/wiremock.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class {{classname}}MockServer {
1818
@Deprecated
1919
{{/isDeprecated}}
2020
public static MappingBuilder stub{{#lambda.pascalcase}}{{operationId}}{{/lambda.pascalcase}}{{{code}}}({{#allParams}}{{#required}}{{#isNullable}}@{{javaxPackage}}.annotation.Nullable {{/isNullable}}{{^isNullable}}@{{javaxPackage}}.annotation.Nonnull {{/isNullable}}{{/required}}{{^required}}@{{javaxPackage}}.annotation.Nullable {{/required}}{{^isBodyParam}}String {{paramName}}{{/isBodyParam}}{{#isBodyParam}}String body{{/isBodyParam}}{{^-last}}, {{/-last}}{{#-last}}{{#headers.0}}, {{/headers.0}}{{^headers.0}}{{#returnType}}, {{/returnType}}{{/headers.0}}{{/-last}}{{/allParams}}{{#headers}}String response{{#lambda.pascalcase}}{{baseName}}{{/lambda.pascalcase}}{{^-last}}, {{/-last}}{{#-last}}{{#returnType}}, {{/returnType}}{{/-last}}{{/headers}}{{#returnType}}String response{{/returnType}}) {
21-
MappingBuilder stub = {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}({{^pathParams.0}}urlPathEqualTo{{/pathParams.0}}{{#pathParams.0}}urlPathTemplate{{/pathParams.0}}("{{{path}}}")){{#hasProduces}}
22-
.withHeader("Accept", havingExactly({{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}})){{/hasProduces}}{{#hasAuthMethods}}{{#hasConsumes}}
23-
.withHeader("Content-Type", havingExactly("{{#consumes.0}}{{{mediaType}}}{{/consumes.0}}")){{/hasConsumes}}
21+
MappingBuilder stub = {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}({{^pathParams.0}}urlPathEqualTo{{/pathParams.0}}{{#pathParams.0}}urlPathTemplate{{/pathParams.0}}("{{{path}}}")){{#hasProduces}}{{#produces}}
22+
.withHeader("Accept", containing("{{{mediaType}}}")){{/produces}}{{/hasProduces}}{{#hasAuthMethods}}{{#hasConsumes}}
23+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("{{#consumes.0}}{{{mediaType}}}{{/consumes.0}}"))){{/hasConsumes}}
2424
.withHeader("Authorization", matching(".*")){{/hasAuthMethods}}{{#cookieParams}}
2525
.withCookie("{{baseName}}", havingExactly({{paramName}})){{/cookieParams}}{{#hasBodyParam}}
2626
.withRequestBody(equalToJson(body)){{/hasBodyParam}}
@@ -105,9 +105,9 @@ public class {{classname}}MockServer {
105105
{{/responses}}
106106

107107
public static MappingBuilder stub{{#lambda.pascalcase}}{{operationId}}{{/lambda.pascalcase}}Fault({{#allParams}}{{#required}}{{#isNullable}}@{{javaxPackage}}.annotation.Nullable {{/isNullable}}{{^isNullable}}@{{javaxPackage}}.annotation.Nonnull {{/isNullable}}{{/required}}{{^required}}@{{javaxPackage}}.annotation.Nullable {{/required}}{{^isBodyParam}}String {{paramName}}{{/isBodyParam}}{{#isBodyParam}}String body{{/isBodyParam}}, {{/allParams}}Fault fault) {
108-
MappingBuilder stub = {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}({{^pathParams.0}}urlPathEqualTo{{/pathParams.0}}{{#pathParams.0}}urlPathTemplate{{/pathParams.0}}("{{{path}}}")){{#hasProduces}}
109-
.withHeader("Accept", havingExactly({{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}})){{/hasProduces}}{{#hasAuthMethods}}{{#hasConsumes}}
110-
.withHeader("Content-Type", havingExactly("{{#consumes.0}}{{{mediaType}}}{{/consumes.0}}")){{/hasConsumes}}
108+
MappingBuilder stub = {{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}({{^pathParams.0}}urlPathEqualTo{{/pathParams.0}}{{#pathParams.0}}urlPathTemplate{{/pathParams.0}}("{{{path}}}")){{#hasProduces}}{{#produces}}
109+
.withHeader("Accept", containing("{{{mediaType}}}")){{/produces}}{{/hasProduces}}{{#hasAuthMethods}}{{#hasConsumes}}
110+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("{{#consumes.0}}{{{mediaType}}}{{/consumes.0}}"))){{/hasConsumes}}
111111
.withHeader("Authorization", matching(".*")){{/hasAuthMethods}}{{#cookieParams}}
112112
.withCookie("{{baseName}}", havingExactly({{paramName}})){{/cookieParams}}{{#bodyParam}}
113113
.withRequestBody(equalToJson(body)){{/bodyParam}}

samples/server/petstore/java-wiremock/src/main/java/org/openapitools/mockserver/api/AnotherFakeApiMockServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AnotherFakeApiMockServer {
1313

1414
public static MappingBuilder stubCall123testSpecialTags200(@javax.annotation.Nonnull String body, String response) {
1515
MappingBuilder stub = patch(urlPathEqualTo("/another-fake/dummy"))
16-
.withHeader("Accept", havingExactly("application/json"))
16+
.withHeader("Accept", containing("application/json"))
1717
.withRequestBody(equalToJson(body))
1818
.willReturn(aResponse()
1919
.withStatus(200)
@@ -27,7 +27,7 @@ public static MappingBuilder stubCall123testSpecialTags200(@javax.annotation.Non
2727

2828
public static MappingBuilder stubCall123testSpecialTagsFault(@javax.annotation.Nonnull String body, Fault fault) {
2929
MappingBuilder stub = patch(urlPathEqualTo("/another-fake/dummy"))
30-
.withHeader("Accept", havingExactly("application/json"))
30+
.withHeader("Accept", containing("application/json"))
3131
.withRequestBody(equalToJson(body))
3232
.willReturn(aResponse()
3333
.withFault(fault)

samples/server/petstore/java-wiremock/src/main/java/org/openapitools/mockserver/api/DefaultApiMockServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DefaultApiMockServer {
1313

1414
public static MappingBuilder stubFooGet0(String response) {
1515
MappingBuilder stub = get(urlPathEqualTo("/foo"))
16-
.withHeader("Accept", havingExactly("application/json"))
16+
.withHeader("Accept", containing("application/json"))
1717
.willReturn(aResponse()
1818
.withStatus(0)
1919
.withHeader("Content-Type", "application/json")
@@ -26,7 +26,7 @@ public static MappingBuilder stubFooGet0(String response) {
2626

2727
public static MappingBuilder stubFooGetFault(Fault fault) {
2828
MappingBuilder stub = get(urlPathEqualTo("/foo"))
29-
.withHeader("Accept", havingExactly("application/json"))
29+
.withHeader("Accept", containing("application/json"))
3030
.willReturn(aResponse()
3131
.withFault(fault)
3232
);

samples/server/petstore/java-wiremock/src/main/java/org/openapitools/mockserver/api/FakeApiMockServer.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FakeApiMockServer {
1313

1414
public static MappingBuilder stubFakeBigDecimalMap200(String response) {
1515
MappingBuilder stub = get(urlPathEqualTo("/fake/BigDecimalMap"))
16-
.withHeader("Accept", havingExactly("*/*"))
16+
.withHeader("Accept", containing("*/*"))
1717
.willReturn(aResponse()
1818
.withStatus(200)
1919
.withHeader("Content-Type", "*/*")
@@ -26,7 +26,7 @@ public static MappingBuilder stubFakeBigDecimalMap200(String response) {
2626

2727
public static MappingBuilder stubFakeBigDecimalMapFault(Fault fault) {
2828
MappingBuilder stub = get(urlPathEqualTo("/fake/BigDecimalMap"))
29-
.withHeader("Accept", havingExactly("*/*"))
29+
.withHeader("Accept", containing("*/*"))
3030
.willReturn(aResponse()
3131
.withFault(fault)
3232
);
@@ -43,7 +43,7 @@ public static String fakeBigDecimalMap200ResponseSample1() {
4343

4444
public static MappingBuilder stubFakeHealthGet200(String response) {
4545
MappingBuilder stub = get(urlPathEqualTo("/fake/health"))
46-
.withHeader("Accept", havingExactly("application/json"))
46+
.withHeader("Accept", containing("application/json"))
4747
.willReturn(aResponse()
4848
.withStatus(200)
4949
.withHeader("Content-Type", "application/json")
@@ -56,7 +56,7 @@ public static MappingBuilder stubFakeHealthGet200(String response) {
5656

5757
public static MappingBuilder stubFakeHealthGetFault(Fault fault) {
5858
MappingBuilder stub = get(urlPathEqualTo("/fake/health"))
59-
.withHeader("Accept", havingExactly("application/json"))
59+
.withHeader("Accept", containing("application/json"))
6060
.willReturn(aResponse()
6161
.withFault(fault)
6262
);
@@ -73,7 +73,7 @@ public static String fakeHealthGet200ResponseSample1() {
7373

7474
public static MappingBuilder stubFakeHttpSignatureTest200(@javax.annotation.Nonnull String body, @javax.annotation.Nullable String query1, @javax.annotation.Nullable String header1) {
7575
MappingBuilder stub = get(urlPathEqualTo("/fake/http-signature-test"))
76-
.withHeader("Content-Type", havingExactly("application/json"))
76+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/json")))
7777
.withHeader("Authorization", matching(".*"))
7878
.withRequestBody(equalToJson(body))
7979
.willReturn(aResponse()
@@ -92,7 +92,7 @@ public static MappingBuilder stubFakeHttpSignatureTest200(@javax.annotation.Nonn
9292

9393
public static MappingBuilder stubFakeHttpSignatureTestFault(@javax.annotation.Nonnull String body, @javax.annotation.Nullable String query1, @javax.annotation.Nullable String header1, Fault fault) {
9494
MappingBuilder stub = get(urlPathEqualTo("/fake/http-signature-test"))
95-
.withHeader("Content-Type", havingExactly("application/json"))
95+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/json")))
9696
.withHeader("Authorization", matching(".*"))
9797
.withRequestBody(equalToJson(body))
9898
.willReturn(aResponse()
@@ -120,7 +120,7 @@ public static String fakeHttpSignatureTestRequestSample2() {
120120

121121
public static MappingBuilder stubFakeOuterBooleanSerialize200(@javax.annotation.Nullable String body, String response) {
122122
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/boolean"))
123-
.withHeader("Accept", havingExactly("*/*"))
123+
.withHeader("Accept", containing("*/*"))
124124
.withRequestBody(equalToJson(body))
125125
.willReturn(aResponse()
126126
.withStatus(200)
@@ -134,7 +134,7 @@ public static MappingBuilder stubFakeOuterBooleanSerialize200(@javax.annotation.
134134

135135
public static MappingBuilder stubFakeOuterBooleanSerializeFault(@javax.annotation.Nullable String body, Fault fault) {
136136
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/boolean"))
137-
.withHeader("Accept", havingExactly("*/*"))
137+
.withHeader("Accept", containing("*/*"))
138138
.withRequestBody(equalToJson(body))
139139
.willReturn(aResponse()
140140
.withFault(fault)
@@ -152,7 +152,7 @@ public static String fakeOuterBooleanSerializeRequestSample1() {
152152

153153
public static MappingBuilder stubFakeOuterCompositeSerialize200(@javax.annotation.Nullable String body, String response) {
154154
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/composite"))
155-
.withHeader("Accept", havingExactly("*/*"))
155+
.withHeader("Accept", containing("*/*"))
156156
.withRequestBody(equalToJson(body))
157157
.willReturn(aResponse()
158158
.withStatus(200)
@@ -166,7 +166,7 @@ public static MappingBuilder stubFakeOuterCompositeSerialize200(@javax.annotatio
166166

167167
public static MappingBuilder stubFakeOuterCompositeSerializeFault(@javax.annotation.Nullable String body, Fault fault) {
168168
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/composite"))
169-
.withHeader("Accept", havingExactly("*/*"))
169+
.withHeader("Accept", containing("*/*"))
170170
.withRequestBody(equalToJson(body))
171171
.willReturn(aResponse()
172172
.withFault(fault)
@@ -187,7 +187,7 @@ public static String fakeOuterCompositeSerializeRequestSample1() {
187187

188188
public static MappingBuilder stubFakeOuterNumberSerialize200(@javax.annotation.Nullable String body, String response) {
189189
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/number"))
190-
.withHeader("Accept", havingExactly("*/*"))
190+
.withHeader("Accept", containing("*/*"))
191191
.withRequestBody(equalToJson(body))
192192
.willReturn(aResponse()
193193
.withStatus(200)
@@ -201,7 +201,7 @@ public static MappingBuilder stubFakeOuterNumberSerialize200(@javax.annotation.N
201201

202202
public static MappingBuilder stubFakeOuterNumberSerializeFault(@javax.annotation.Nullable String body, Fault fault) {
203203
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/number"))
204-
.withHeader("Accept", havingExactly("*/*"))
204+
.withHeader("Accept", containing("*/*"))
205205
.withRequestBody(equalToJson(body))
206206
.willReturn(aResponse()
207207
.withFault(fault)
@@ -219,7 +219,7 @@ public static String fakeOuterNumberSerializeRequestSample1() {
219219

220220
public static MappingBuilder stubFakeOuterStringSerialize200(@javax.annotation.Nullable String body, String response) {
221221
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/string"))
222-
.withHeader("Accept", havingExactly("*/*"))
222+
.withHeader("Accept", containing("*/*"))
223223
.withRequestBody(equalToJson(body))
224224
.willReturn(aResponse()
225225
.withStatus(200)
@@ -233,7 +233,7 @@ public static MappingBuilder stubFakeOuterStringSerialize200(@javax.annotation.N
233233

234234
public static MappingBuilder stubFakeOuterStringSerializeFault(@javax.annotation.Nullable String body, Fault fault) {
235235
MappingBuilder stub = post(urlPathEqualTo("/fake/outer/string"))
236-
.withHeader("Accept", havingExactly("*/*"))
236+
.withHeader("Accept", containing("*/*"))
237237
.withRequestBody(equalToJson(body))
238238
.willReturn(aResponse()
239239
.withFault(fault)
@@ -251,7 +251,7 @@ public static String fakeOuterStringSerializeRequestSample1() {
251251

252252
public static MappingBuilder stubFakePropertyEnumIntegerSerialize200(@javax.annotation.Nonnull String body, String response) {
253253
MappingBuilder stub = post(urlPathEqualTo("/fake/property/enum-int"))
254-
.withHeader("Accept", havingExactly("*/*"))
254+
.withHeader("Accept", containing("*/*"))
255255
.withRequestBody(equalToJson(body))
256256
.willReturn(aResponse()
257257
.withStatus(200)
@@ -265,7 +265,7 @@ public static MappingBuilder stubFakePropertyEnumIntegerSerialize200(@javax.anno
265265

266266
public static MappingBuilder stubFakePropertyEnumIntegerSerializeFault(@javax.annotation.Nonnull String body, Fault fault) {
267267
MappingBuilder stub = post(urlPathEqualTo("/fake/property/enum-int"))
268-
.withHeader("Accept", havingExactly("*/*"))
268+
.withHeader("Accept", containing("*/*"))
269269
.withRequestBody(equalToJson(body))
270270
.willReturn(aResponse()
271271
.withFault(fault)
@@ -400,7 +400,7 @@ public static String testBodyWithQueryParamsRequestSample1() {
400400

401401
public static MappingBuilder stubTestClientModel200(@javax.annotation.Nonnull String body, String response) {
402402
MappingBuilder stub = patch(urlPathEqualTo("/fake"))
403-
.withHeader("Accept", havingExactly("application/json"))
403+
.withHeader("Accept", containing("application/json"))
404404
.withRequestBody(equalToJson(body))
405405
.willReturn(aResponse()
406406
.withStatus(200)
@@ -414,7 +414,7 @@ public static MappingBuilder stubTestClientModel200(@javax.annotation.Nonnull St
414414

415415
public static MappingBuilder stubTestClientModelFault(@javax.annotation.Nonnull String body, Fault fault) {
416416
MappingBuilder stub = patch(urlPathEqualTo("/fake"))
417-
.withHeader("Accept", havingExactly("application/json"))
417+
.withHeader("Accept", containing("application/json"))
418418
.withRequestBody(equalToJson(body))
419419
.willReturn(aResponse()
420420
.withFault(fault)
@@ -435,7 +435,7 @@ public static String testClientModelRequestSample1() {
435435

436436
public static MappingBuilder stubTestEndpointParameters400(@javax.annotation.Nonnull String number, @javax.annotation.Nonnull String _double, @javax.annotation.Nonnull String patternWithoutDelimiter, @javax.annotation.Nonnull String _byte, @javax.annotation.Nullable String integer, @javax.annotation.Nullable String int32, @javax.annotation.Nullable String int64, @javax.annotation.Nullable String _float, @javax.annotation.Nullable String string, @javax.annotation.Nullable String binary, @javax.annotation.Nullable String date, @javax.annotation.Nullable String dateTime, @javax.annotation.Nullable String password, @javax.annotation.Nullable String paramCallback) {
437437
MappingBuilder stub = post(urlPathEqualTo("/fake"))
438-
.withHeader("Content-Type", havingExactly("application/x-www-form-urlencoded"))
438+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/x-www-form-urlencoded")))
439439
.withHeader("Authorization", matching(".*"))
440440
.willReturn(aResponse()
441441
.withStatus(400)
@@ -481,7 +481,7 @@ public static MappingBuilder stubTestEndpointParameters400(@javax.annotation.Non
481481

482482
public static MappingBuilder stubTestEndpointParameters404(@javax.annotation.Nonnull String number, @javax.annotation.Nonnull String _double, @javax.annotation.Nonnull String patternWithoutDelimiter, @javax.annotation.Nonnull String _byte, @javax.annotation.Nullable String integer, @javax.annotation.Nullable String int32, @javax.annotation.Nullable String int64, @javax.annotation.Nullable String _float, @javax.annotation.Nullable String string, @javax.annotation.Nullable String binary, @javax.annotation.Nullable String date, @javax.annotation.Nullable String dateTime, @javax.annotation.Nullable String password, @javax.annotation.Nullable String paramCallback) {
483483
MappingBuilder stub = post(urlPathEqualTo("/fake"))
484-
.withHeader("Content-Type", havingExactly("application/x-www-form-urlencoded"))
484+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/x-www-form-urlencoded")))
485485
.withHeader("Authorization", matching(".*"))
486486
.willReturn(aResponse()
487487
.withStatus(404)
@@ -527,7 +527,7 @@ public static MappingBuilder stubTestEndpointParameters404(@javax.annotation.Non
527527

528528
public static MappingBuilder stubTestEndpointParametersFault(@javax.annotation.Nonnull String number, @javax.annotation.Nonnull String _double, @javax.annotation.Nonnull String patternWithoutDelimiter, @javax.annotation.Nonnull String _byte, @javax.annotation.Nullable String integer, @javax.annotation.Nullable String int32, @javax.annotation.Nullable String int64, @javax.annotation.Nullable String _float, @javax.annotation.Nullable String string, @javax.annotation.Nullable String binary, @javax.annotation.Nullable String date, @javax.annotation.Nullable String dateTime, @javax.annotation.Nullable String password, @javax.annotation.Nullable String paramCallback, Fault fault) {
529529
MappingBuilder stub = post(urlPathEqualTo("/fake"))
530-
.withHeader("Content-Type", havingExactly("application/x-www-form-urlencoded"))
530+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/x-www-form-urlencoded")))
531531
.withHeader("Authorization", matching(".*"))
532532
.willReturn(aResponse()
533533
.withFault(fault)

samples/server/petstore/java-wiremock/src/main/java/org/openapitools/mockserver/api/FakeClassnameTags123ApiMockServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class FakeClassnameTags123ApiMockServer {
1313

1414
public static MappingBuilder stubTestClassname200(@javax.annotation.Nonnull String body, String response) {
1515
MappingBuilder stub = patch(urlPathEqualTo("/fake_classname_test"))
16-
.withHeader("Accept", havingExactly("application/json"))
17-
.withHeader("Content-Type", havingExactly("application/json"))
16+
.withHeader("Accept", containing("application/json"))
17+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/json")))
1818
.withHeader("Authorization", matching(".*"))
1919
.withRequestBody(equalToJson(body))
2020
.willReturn(aResponse()
@@ -29,8 +29,8 @@ public static MappingBuilder stubTestClassname200(@javax.annotation.Nonnull Stri
2929

3030
public static MappingBuilder stubTestClassnameFault(@javax.annotation.Nonnull String body, Fault fault) {
3131
MappingBuilder stub = patch(urlPathEqualTo("/fake_classname_test"))
32-
.withHeader("Accept", havingExactly("application/json"))
33-
.withHeader("Content-Type", havingExactly("application/json"))
32+
.withHeader("Accept", containing("application/json"))
33+
.withHeader("Content-Type", havingExactly(equalToIgnoreCase("application/json")))
3434
.withHeader("Authorization", matching(".*"))
3535
.withRequestBody(equalToJson(body))
3636
.willReturn(aResponse()

0 commit comments

Comments
 (0)