From 0897a4f4c0fbbb05b9348ba84458cf4e4ede69d0 Mon Sep 17 00:00:00 2001 From: Renuka Fernando Date: Sat, 31 Aug 2024 16:11:44 +0530 Subject: [PATCH] Set appropriate statusCode from examples Issue: OpenAPITools/openapi-generator#19446 --- .../src/main/resources/JavaSpring/api.mustache | 3 +++ .../src/main/resources/JavaSpring/methodBody.mustache | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache index d6609ff960c0..dfd77f7fd592 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache @@ -79,6 +79,9 @@ import java.util.Optional; {{#async}} import java.util.concurrent.CompletableFuture; {{/async}} +{{#returnSuccessCode}} +import java.util.concurrent.atomic.AtomicInteger; +{{/returnSuccessCode}} import {{javaxPackage}}.annotation.Generated; {{>generatedAnnotation}} diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache index bbbc66de3975..57045774d386 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache @@ -3,18 +3,20 @@ {{#-first}} {{#async}} return CompletableFuture.supplyAsync(()-> { - {{/async}}getRequest().ifPresent(request -> { + {{/async}}{{#returnSuccessCode}}AtomicInteger statusCode = new AtomicInteger(501); + {{/returnSuccessCode}}getRequest().ifPresent(request -> { {{#async}} {{/async}} for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { {{/-first}} {{#async}} {{/async}}{{^async}} {{/async}} if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) { {{#async}} {{/async}}{{^async}} {{/async}} String exampleString = {{>exampleString}}; -{{#async}} {{/async}}{{^async}} {{/async}} ApiUtil.setExampleResponse(request, "{{{contentType}}}", exampleString); +{{#async}} {{/async}}{{^async}} {{/async}} ApiUtil.setExampleResponse(request, "{{{contentType}}}", exampleString);{{#returnSuccessCode}} +{{#async}} {{/async}}{{^async}} {{/async}} statusCode.set({{{statusCode}}});{{/returnSuccessCode}} {{#async}} {{/async}}{{^async}} {{/async}} break; {{#async}} {{/async}}{{^async}} {{/async}} } {{#-last}} {{#async}} {{/async}}{{^async}} {{/async}} } {{#async}} {{/async}} }); -{{#async}} {{/async}} {{#useResponseEntity}}return new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.valueOf({{{statusCode}}}){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}); +{{#async}} {{/async}} {{#useResponseEntity}}return new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.valueOf(statusCode.get()){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}); {{/useResponseEntity}} {{^useResponseEntity}}throw new IllegalArgumentException("Not implemented"); {{/useResponseEntity}}