diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache index 2b7662531379..dcc950cdeb75 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache @@ -76,7 +76,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v operationId = "{{{operationId}}}", description = """{{{unescapedNotes}}}""", responses = [{{#responses}} - ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = [Content({{#isArray}}array = ArraySchema({{/isArray}}schema = Schema(implementation = {{{baseType}}}::class)){{#isArray}}){{/isArray}}]{{/baseType}}){{^-last}},{{/-last}}{{/responses}} ]{{#hasAuthMethods}}, + ApiResponse(responseCode = "{{#isDefault}}default{{/isDefault}}{{^isDefault}}{{{code}}}{{/isDefault}}", description = "{{{message}}}"{{#baseType}}, content = [Content({{#isArray}}array = ArraySchema({{/isArray}}schema = Schema(implementation = {{{baseType}}}::class)){{#isArray}}){{/isArray}}]{{/baseType}}){{^-last}},{{/-last}}{{/responses}} ]{{#hasAuthMethods}}, security = [ {{#authMethods}}SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = [ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} ]{{/isOAuth}}){{^-last}},{{/-last}}{{/authMethods}} ]{{/hasAuthMethods}} ){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}} @ApiOperation( diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache index 82608294c6ba..851b07427a6f 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache @@ -88,7 +88,7 @@ interface {{classname}} { operationId = "{{{operationId}}}", description = """{{{unescapedNotes}}}""", responses = [{{#responses}} - ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = [Content({{#isArray}}array = ArraySchema({{/isArray}}schema = Schema(implementation = {{{baseType}}}::class)){{#isArray}}){{/isArray}}]{{/baseType}}){{^-last}},{{/-last}}{{/responses}} + ApiResponse(responseCode = "{{#isDefault}}default{{/isDefault}}{{^isDefault}}{{{code}}}{{/isDefault}}", description = "{{{message}}}"{{#baseType}}, content = [Content({{#isArray}}array = ArraySchema({{/isArray}}schema = Schema(implementation = {{{baseType}}}::class)){{#isArray}}){{/isArray}}]{{/baseType}}){{^-last}},{{/-last}}{{/responses}} ]{{#hasAuthMethods}}, security = [ {{#authMethods}}SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = [ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} ]{{/isOAuth}}){{^-last}},{{/-last}}{{/authMethods}} ]{{/hasAuthMethods}} ){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java index 0e83e957927d..d375c88d01a2 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java @@ -3021,6 +3021,41 @@ public void nonReactiveWithResponseEntity() throws Exception { ); } + /** + * Regression test for https://github.com/OpenAPITools/openapi-generator/issues/17445. + * OpenAPI 'default' responses must emit responseCode = "default" in @ApiResponse (swagger2), + * not "0" (internal pre-processed value) or "200" (incorrect mapping from parent codegen). + * Also verifies that useResponseEntity=false does not crash when the first response is 'default'. + */ + @Test + public void defaultResponseCodeRenderedAsDefault() throws Exception { + Path root = generateApiSources(Map.of( + KotlinSpringServerCodegen.REACTIVE, false, + KotlinSpringServerCodegen.ANNOTATION_LIBRARY, "swagger2", + KotlinSpringServerCodegen.INTERFACE_ONLY, true, + KotlinSpringServerCodegen.USE_RESPONSE_ENTITY, false + ), Map.of( + CodegenConstants.MODELS, "false", + CodegenConstants.MODEL_TESTS, "false", + CodegenConstants.MODEL_DOCS, "false", + CodegenConstants.APIS, "true", + CodegenConstants.SUPPORTING_FILES, "false" + )); + Path userApi = root.resolve("src/main/kotlin/org/openapitools/api/UserApi.kt"); + // operations whose only OpenAPI response is 'default:' must use responseCode = "default" + assertFileContains(userApi, + "ApiResponse(responseCode = \"default\", description = \"successful operation\")" + ); + // explicit HTTP 200 responses must still use the concrete status code + assertFileContains(userApi, + "ApiResponse(responseCode = \"200\", description = \"successful operation\", content" + ); + // the raw internal representation ("0") must never appear in generated output + assertFileNotContains(userApi, + "responseCode = \"0\"" + ); + } + @Test public void reactiveWithoutFlow() throws Exception { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); diff --git a/samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/UserApiController.kt b/samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/UserApiController.kt index b59055789659..779ab20356e1 100644 --- a/samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/UserApiController.kt +++ b/samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/UserApiController.kt @@ -38,7 +38,7 @@ class UserApiController() { operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -58,7 +58,7 @@ class UserApiController() { operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -78,7 +78,7 @@ class UserApiController() { operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -160,7 +160,7 @@ class UserApiController() { operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( diff --git a/samples/server/petstore/kotlin-springboot-delegate-nodefaults/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-delegate-nodefaults/src/main/kotlin/org/openapitools/api/UserApi.kt index fff346fae41b..4bce0a3890ff 100644 --- a/samples/server/petstore/kotlin-springboot-delegate-nodefaults/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-delegate-nodefaults/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -46,7 +46,7 @@ interface UserApi { operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -68,7 +68,7 @@ interface UserApi { operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -90,7 +90,7 @@ interface UserApi { operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -180,7 +180,7 @@ interface UserApi { operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) diff --git a/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApi.kt index 5c6b0adfc1a2..ad3e776b029e 100644 --- a/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -45,7 +45,7 @@ interface UserApi { operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -67,7 +67,7 @@ interface UserApi { operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -89,7 +89,7 @@ interface UserApi { operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @@ -179,7 +179,7 @@ interface UserApi { operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) diff --git a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiController.kt b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiController.kt index 7dd168a5cf20..166e2764f7c2 100644 --- a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiController.kt +++ b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiController.kt @@ -38,7 +38,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -56,7 +56,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -74,7 +74,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -153,7 +153,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.GET], diff --git a/samples/server/petstore/kotlin-springboot-reactive-without-flow/src/main/kotlin/org/openapitools/api/UserApiController.kt b/samples/server/petstore/kotlin-springboot-reactive-without-flow/src/main/kotlin/org/openapitools/api/UserApiController.kt index d677e66a952f..e81a31b5697d 100644 --- a/samples/server/petstore/kotlin-springboot-reactive-without-flow/src/main/kotlin/org/openapitools/api/UserApiController.kt +++ b/samples/server/petstore/kotlin-springboot-reactive-without-flow/src/main/kotlin/org/openapitools/api/UserApiController.kt @@ -39,7 +39,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -59,7 +59,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -79,7 +79,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -161,7 +161,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( diff --git a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApiController.kt b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApiController.kt index d677e66a952f..e81a31b5697d 100644 --- a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApiController.kt +++ b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApiController.kt @@ -39,7 +39,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -59,7 +59,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -79,7 +79,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( @@ -161,7 +161,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ], + ApiResponse(responseCode = "default", description = "successful operation") ], security = [ SecurityRequirement(name = "api_key") ] ) @RequestMapping( diff --git a/samples/server/petstore/kotlin-springboot-request-cookie/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-request-cookie/src/main/kotlin/org/openapitools/api/UserApi.kt index 37506ccbab0d..38412b1067c6 100644 --- a/samples/server/petstore/kotlin-springboot-request-cookie/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-request-cookie/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -43,7 +43,7 @@ interface UserApi { operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( @@ -64,7 +64,7 @@ interface UserApi { operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( @@ -85,7 +85,7 @@ interface UserApi { operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( @@ -173,7 +173,7 @@ interface UserApi { operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( diff --git a/samples/server/petstore/kotlin-springboot-source-swagger2/src/main/kotlin/org/openapitools/api/UserApiController.kt b/samples/server/petstore/kotlin-springboot-source-swagger2/src/main/kotlin/org/openapitools/api/UserApiController.kt index 7dd168a5cf20..166e2764f7c2 100644 --- a/samples/server/petstore/kotlin-springboot-source-swagger2/src/main/kotlin/org/openapitools/api/UserApiController.kt +++ b/samples/server/petstore/kotlin-springboot-source-swagger2/src/main/kotlin/org/openapitools/api/UserApiController.kt @@ -38,7 +38,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUser", description = """This can only be done by the logged in user.""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -56,7 +56,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithArrayInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -74,7 +74,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "createUsersWithListInput", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.POST], @@ -153,7 +153,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService) operationId = "logoutUser", description = """""", responses = [ - ApiResponse(responseCode = "200", description = "successful operation") ] + ApiResponse(responseCode = "default", description = "successful operation") ] ) @RequestMapping( method = [RequestMethod.GET],