Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix][kotlin-wiremock] fix problems with range responses #19309

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/samples-kotlin-wiremock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Samples Kotlin WireMock
on:
push:
branches:
- samples/server/petstore/kotlin-wiremock/**
- samples/server/petstore/kotlin-wiremock*/**
- samples/server/echo_api/kotlin-wiremock/**
pull_request:
paths:
- samples/server/petstore/kotlin-wiremock/**
- samples/server/petstore/kotlin-wiremock*/**
- samples/server/echo_api/kotlin-wiremock/**

jobs:
Expand All @@ -19,6 +19,7 @@ jobs:
matrix:
sample:
- samples/server/petstore/kotlin-wiremock
- samples/server/petstore/kotlin-wiremock-responses
stefankoppier marked this conversation as resolved.
Show resolved Hide resolved
- samples/server/echo_api/kotlin-wiremock
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 4 additions & 0 deletions bin/configs/kotlin-wiremock-responses.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: kotlin-wiremock
outputDir: samples/server/petstore/kotlin-wiremock-responses
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue7193-responses.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-wiremock
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.openapitools.codegen.languages;

import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.meta.features.GlobalFeature;
Expand All @@ -16,13 +14,16 @@
import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class KotlinWiremockServerCodegen extends AbstractKotlinCodegen {

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

protected static final String VENDOR_EXTENSION_IS_RANGE_RESPONSE_CODE = "x-is-range-code";

@Override
public CodegenType getTag() {
return CodegenType.SERVER;
Expand Down Expand Up @@ -126,6 +127,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
return objects;
}

@Override
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
var r = super.fromResponse(responseCode, response);

var isRange = List.of("1xx", "2xx", "3xx", "4xx", "5xx").contains(responseCode.toLowerCase(Locale.ROOT));
r.vendorExtensions.put(VENDOR_EXTENSION_IS_RANGE_RESPONSE_CODE, isRange);
return r;
}

@Override
public void postProcess() {
System.out.println("################################################################################");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,68 @@ import {{modelPackage}}.*
class {{operationIdCamelCase}}StubBuilder internal constructor(private val objectMapper: ObjectMapper, private val stub: MappingBuilder) {
{{#responses}}
{{^wildcard}}
{{^vendorExtensions.x-is-range-code}}

/**
* Let the stub for {{operationId}} respond with HTTP status code {{code}}.
*
{{#returnType}}
{{#dataType}}
* @param body response body for the [MappingBuilder].
{{/returnType}}
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
{{/dataType}}
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith{{code}}(
{{#returnType}}
{{#dataType}}
body: {{{.}}},
{{/returnType}}
{{/dataType}}
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this },
): MappingBuilder =
stub.willReturn(aResponse()
.withStatus({{code}})
{{#returnType}}
{{#dataType}}
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(body))
{{/returnType}}
{{/dataType}}
.configurer()
)
{{/vendorExtensions.x-is-range-code}}
{{/wildcard}}
{{#vendorExtensions.x-is-range-code}}

/**
* Let the stub for {{operationId}} respond with HTTP status code [code].
*
* @param code the response code.
{{#dataType}}
* @param body response body for the [MappingBuilder].
{{/dataType}}
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith{{code}}(
code: Int,
{{#dataType}}
body: {{{.}}},
{{/dataType}}
configurer: ResponseDefinitionBuilder.() -> ResponseDefinitionBuilder = { this },
): MappingBuilder =
stub.willReturn(aResponse()
.withStatus(code)
{{#dataType}}
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(body))
{{/dataType}}
.configurer()
)
{{/vendorExtensions.x-is-range-code}}
{{/responses}}

/**
* Let the stub for {{operationId}} respond with HTTP status code [code].
*
{{#returnType}}
* @param code the response code.
* @param body response body for the [MappingBuilder].
{{/returnType}}
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.0.0
info:
description: >-
This spec is mainly for testing Petstore server and contains fake endpoints,
models. Please do not use this for any other purpose. Special characters: "
\
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
/foo:
get:
responses:
default:
description: response
content:
application/json:
schema:
type: number
4XX:
description: client error
content:
application/json:
schema:
$ref: '#/components/schemas/Foo'
404:
description: not found
content:
application/json:
schema:
type: string
400:
description: bad request
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Foo'
components:
schemas:
Foo:
type: object
properties:
bar:
$ref: '#/components/schemas/Bar'
Bar:
type: string
default: bar
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestAuthHttpBasicStubBuilder internal constructor(private val objectMapper
* Let the stub for testAuthHttpBasic respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -39,6 +39,7 @@ class TestAuthHttpBasicStubBuilder internal constructor(private val objectMapper
/**
* Let the stub for testAuthHttpBasic respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -69,7 +70,7 @@ class TestAuthHttpBearerStubBuilder internal constructor(private val objectMappe
* Let the stub for testAuthHttpBearer respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -86,6 +87,7 @@ class TestAuthHttpBearerStubBuilder internal constructor(private val objectMappe
/**
* Let the stub for testAuthHttpBearer respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestBinaryGifStubBuilder internal constructor(private val objectMapper: Ob
* Let the stub for testBinaryGif respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -39,6 +39,7 @@ class TestBinaryGifStubBuilder internal constructor(private val objectMapper: Ob
/**
* Let the stub for testBinaryGif respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -69,7 +70,7 @@ class TestBodyApplicationOctetstreamBinaryStubBuilder internal constructor(priva
* Let the stub for testBodyApplicationOctetstreamBinary respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -86,6 +87,7 @@ class TestBodyApplicationOctetstreamBinaryStubBuilder internal constructor(priva
/**
* Let the stub for testBodyApplicationOctetstreamBinary respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -116,7 +118,7 @@ class TestBodyMultipartFormdataArrayOfBinaryStubBuilder internal constructor(pri
* Let the stub for testBodyMultipartFormdataArrayOfBinary respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -133,6 +135,7 @@ class TestBodyMultipartFormdataArrayOfBinaryStubBuilder internal constructor(pri
/**
* Let the stub for testBodyMultipartFormdataArrayOfBinary respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -163,7 +166,7 @@ class TestBodyMultipartFormdataSingleBinaryStubBuilder internal constructor(priv
* Let the stub for testBodyMultipartFormdataSingleBinary respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -180,6 +183,7 @@ class TestBodyMultipartFormdataSingleBinaryStubBuilder internal constructor(priv
/**
* Let the stub for testBodyMultipartFormdataSingleBinary respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -210,7 +214,7 @@ class TestEchoBodyFreeFormObjectResponseStringStubBuilder internal constructor(p
* Let the stub for testEchoBodyFreeFormObjectResponseString respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -227,6 +231,7 @@ class TestEchoBodyFreeFormObjectResponseStringStubBuilder internal constructor(p
/**
* Let the stub for testEchoBodyFreeFormObjectResponseString respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -257,7 +262,7 @@ class TestEchoBodyPetStubBuilder internal constructor(private val objectMapper:
* Let the stub for testEchoBodyPet respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -274,6 +279,7 @@ class TestEchoBodyPetStubBuilder internal constructor(private val objectMapper:
/**
* Let the stub for testEchoBodyPet respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -304,7 +310,7 @@ class TestEchoBodyPetResponseStringStubBuilder internal constructor(private val
* Let the stub for testEchoBodyPetResponseString respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -321,6 +327,7 @@ class TestEchoBodyPetResponseStringStubBuilder internal constructor(private val
/**
* Let the stub for testEchoBodyPetResponseString respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down Expand Up @@ -351,7 +358,7 @@ class TestEchoBodyTagResponseStringStubBuilder internal constructor(private val
* Let the stub for testEchoBodyTagResponseString respond with HTTP status code 200.
*
* @param body response body for the [MappingBuilder].
* @param configurer fonfigurer for the [MappingBuilder], allowing for arbitrary changes.
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
*/
fun respondWith200(
Expand All @@ -368,6 +375,7 @@ class TestEchoBodyTagResponseStringStubBuilder internal constructor(private val
/**
* Let the stub for testEchoBodyTagResponseString respond with HTTP status code [code].
*
* @param code the response code.
* @param body response body for the [MappingBuilder].
* @param configurer configurer for the [MappingBuilder], allowing for arbitrary changes.
* @return a [MappingBuilder] to be registered with a WireMock instance.
Expand Down
Loading
Loading