Skip to content

Commit

Permalink
Merge pull request #987 from Backbase/986-form-style-query-params
Browse files Browse the repository at this point in the history
Issue 986 form style query params
  • Loading branch information
walaniam authored Jan 16, 2025
2 parents 8ecb928 + 8ca9f2b commit eb5bf17
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ public class {{classname}} {
final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}

{{#queryParams}}localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));{{^-last}}
{{/-last}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}
{{#queryParams}}{{#isExplode}}{{#hasVars}}
if ({{paramName}} != null) {
{{#vars}} localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}.{{getter}}()));
{{/vars}}}{{/hasVars}}{{^hasVars}}localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/hasVars}}{{/isExplode}}{{^isExplode}}localVarQueryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{.}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/isExplode}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}

{{#headerParams}}if ({{paramName}} != null)
localVarHeaderParams.add("{{baseName}}", apiClient.parameterToString({{paramName}}));{{^-last}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.backbase.oss.codegen.java;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -19,10 +20,12 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openapitools.codegen.ClientOptInput;
Expand All @@ -49,6 +52,42 @@ void shouldGenerateArrayField(String containerDefaultToNull) {
generateAndAssert(springCodeGen(), containerDefaultToNull);
}

@Test
void shouldExplodePojoQueryParam() throws IOException {

BoatJavaCodeGen codegen = javaCodeGenWithLib(RESTTEMPLATE);

var input = new File("src/test/resources/boat-spring/openapi.yaml");
var outputDir = TEST_OUTPUT + "/shouldExplodePojoQueryParam";

codegen.setInputSpec(input.getAbsolutePath());
codegen.setOutputDir(outputDir);

OpenAPI openApiInput = new OpenAPIParser()
.readLocation(input.getAbsolutePath(), null, new ParseOptions())
.getOpenAPI();
var clientOptInput = new ClientOptInput();
clientOptInput.config(codegen);
clientOptInput.openAPI(openApiInput);

List<File> files = new DefaultGenerator().opts(clientOptInput).generate();

File pojosInQueryApi = files.stream()
.filter(it -> it.getName().equals("PojosInQueryApi.java"))
.findFirst()
.orElseThrow();

List<String> lines = Files.readAllLines(pojosInQueryApi.toPath()).stream()
.map(String::trim)
.collect(Collectors.toList());

assertThat(lines)
.contains("public ResponseEntity<Void> getWithPojosInQueryWithHttpInfo(String simpleParam, MyPojo pojoParam) throws RestClientException {")
.contains("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"simpleParam\", simpleParam));")
.contains("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"field1\", pojoParam.getField1()));")
.contains("localVarQueryParams.putAll(apiClient.parameterToMultiValueMap(null, \"field2\", pojoParam.getField2()));");
}

private static BoatJavaCodeGen javaCodeGenWithLib(String library) {
var codeGen = new BoatJavaCodeGen();
codeGen.setLibrary(library);
Expand Down Expand Up @@ -115,7 +154,7 @@ private void assertVariableDeclarator(CompilationUnit requestClass, String field
.findAll(FieldDeclaration.class)
.stream()
.flatMap(field -> field.getChildNodes().stream())
.filter(node -> node instanceof VariableDeclarator)
.filter(VariableDeclarator.class::isInstance)
.map(VariableDeclarator.class::cast)
.filter(declarator -> declarator.getName().getIdentifier().equals(fieldName))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package com.backbase.oss.codegen.java;

import static com.backbase.oss.codegen.java.BoatJavaCodeGen.CREATE_API_COMPONENT;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.REST_TEMPLATE_BEAN_NAME;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_CLASS_LEVEL_BEAN_VALIDATION;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_DEFAULT_API_CLIENT;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_JACKSON_CONVERSION;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_PROTECTED_FIELDS;
import static com.backbase.oss.codegen.java.BoatJavaCodeGen.USE_WITH_MODIFIERS;
import static java.util.stream.Collectors.groupingBy;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.ParseOptions;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.DefaultGenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.function.Function;
import java.util.List;
import java.util.Map;

import static com.backbase.oss.codegen.java.BoatJavaCodeGen.*;
import static java.util.stream.Collectors.groupingBy;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

class BoatJavaCodeGenTests {

static final String PROP_BASE = BoatJavaCodeGenTests.class.getSimpleName() + ".";
Expand Down Expand Up @@ -115,10 +122,10 @@ void processOptsUseProtectedFields() {

@ParameterizedTest
@ValueSource(booleans = {true, false})
void shouldHonourGenerateComponentAnnotation(boolean generate) throws InterruptedException, FileNotFoundException {
void shouldHonourGenerateComponentAnnotation(boolean generate) throws FileNotFoundException {

var input = new File("src/test/resources/boat-spring/openapi.yaml");
var output = TEST_OUTPUT + "/shouldNotGenerateComponentAnnotation/" + String.valueOf(generate);
var output = TEST_OUTPUT + "/shouldNotGenerateComponentAnnotation/" + generate;

final BoatJavaCodeGen gen = new BoatJavaCodeGen();
gen.setOutputDir(output);
Expand Down Expand Up @@ -162,10 +169,10 @@ void shouldHonourGenerateComponentAnnotation(boolean generate) throws Interrupte

@ParameterizedTest
@ValueSource(booleans = {true, false})
void shouldHonourBeanValidationOption(boolean useBeanValidation) throws InterruptedException, FileNotFoundException {
void shouldHonourBeanValidationOption(boolean useBeanValidation) throws FileNotFoundException {

var input = new File("src/test/resources/boat-spring/openapi.yaml");
var output = TEST_OUTPUT + "/shouldHonourBeanValidationOption/" + String.valueOf(useBeanValidation);
var output = TEST_OUTPUT + "/shouldHonourBeanValidationOption/" + useBeanValidation;

final BoatJavaCodeGen gen = new BoatJavaCodeGen();
gen.setOutputDir(output);
Expand Down
41 changes: 41 additions & 0 deletions boat-scaffold/src/test/resources/boat-spring/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,49 @@ paths:
format: number
number-typed-value:
type: number

/use-pojo-as-query-param:
get:
tags:
- pojosInQuery
operationId: getWithPojosInQuery
parameters:
- name: simpleParam
in: query
required: true
style: form
explode: true
schema:
type: string
- name: pojoParam
in: query
required: true
style: form
explode: true
schema:
$ref: "#/components/schemas/MyPojo"
responses:
"200":
description: OK

components:
schemas:
MyPojo:
required:
- field1
- field2
type: object
properties:
field1:
maxLength: 32
minLength: 1
pattern: ^\S.*$
type: string
field2:
maxLength: 50
minLength: 1
pattern: ^\S.*$
type: string
Additions:
title: Additions
type: object
Expand Down

0 comments on commit eb5bf17

Please sign in to comment.