From 9e0badb3d7d227cd4dad621d854f718e565363e5 Mon Sep 17 00:00:00 2001 From: alfabetacain Date: Tue, 8 Dec 2020 07:55:33 +0100 Subject: [PATCH] added support for cookie parameters to jaxrs-spec generator (#8117) --- .../JavaJaxRS/spec/apiMethod.mustache | 2 +- .../JavaJaxRS/spec/cookieParams.mustache | 1 + .../jaxrs/JavaJAXRSSpecServerCodegenTest.java | 27 +++++++++++++++++++ .../src/test/resources/3_0/issue_2908.yaml | 20 ++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/main/resources/JavaJaxRS/spec/cookieParams.mustache create mode 100644 modules/openapi-generator/src/test/resources/3_0/issue_2908.yaml diff --git a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/apiMethod.mustache b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/apiMethod.mustache index 753553bcea56..09db050cd767 100644 --- a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/apiMethod.mustache +++ b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/apiMethod.mustache @@ -11,6 +11,6 @@ @ApiResponses(value = { {{#responses}} @ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#containerType}}, responseContainer = "{{{containerType}}}"{{/containerType}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}} - public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) { + public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) { return Response.ok().entity("magic!").build(); } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/cookieParams.mustache b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/cookieParams.mustache new file mode 100644 index 000000000000..11858cc194d2 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/cookieParams.mustache @@ -0,0 +1 @@ +{{#isCookieParam}}@CookieParam("{{baseName}}"){{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{^isContainer}}{{#defaultValue}} @DefaultValue({{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}){{/defaultValue}}{{/isContainer}} {{#useSwaggerAnnotations}}{{#description}} @ApiParam("{{description}}"){{/description}}{{/useSwaggerAnnotations}} {{{dataType}}} {{paramName}}{{/isCookieParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java index 8e802fd7a0b1..5668a4398bda 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java @@ -346,6 +346,33 @@ public void testGenerateApiWithPreceedingPathParameter_issue1347() throws Except output.deleteOnExit(); } + @Test + public void testGenerateApiWithCookieParameter_issue2908() throws Exception { + Map properties = new HashMap<>(); + properties.put(JavaJAXRSSpecServerCodegen.OPEN_API_SPEC_FILE_LOCATION, "openapi.yml"); + + File output = Files.createTempDirectory("test").toFile(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("jaxrs-spec") + .setAdditionalProperties(properties) + .setInputSpec("src/test/resources/3_0/issue_2908.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(false); + List files = generator.opts(clientOptInput).generate(); + + validateJavaSourceFiles(files); + + TestUtils.ensureContainsFile(files, output, "openapi.yml"); + files.stream().forEach(System.out::println); + TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/SomethingApi.java"); + TestUtils.assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/SomethingApi.java"), "@CookieParam"); + + output.deleteOnExit(); + } + @Test public void addsImportForSetArgument() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_2908.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_2908.yaml new file mode 100644 index 000000000000..09b7c620f832 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_2908.yaml @@ -0,0 +1,20 @@ +openapi: 3.0.1 +info: + title: Issue 2908 - Cookie parameters + description: "Cookie parameters for JAX-RS Spec generator" + version: 0.0.1 + +paths: + /something: + post: + operationId: withCookieParam + parameters: + - name: myCookieParam + in: cookie + required: true + description: cookie param + schema: + type: string + responses: + 200: + description: success \ No newline at end of file