Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,24 @@ private static boolean isMultipartType(List<Map<String, String>> consumes) {
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
adjustEnumRefDefault(parameter);
propagateParamBaseNameToVars(parameter);
}

/**
* For query parameters with `type: object, properties: ...`, expose the
* parameter's OAS baseName on each generated field via the
* `x-kotlin-param-base-name` vendor extension. Templates that iterate
* `vars` (e.g. jvm-ktor) need the outer baseName to build URL keys like
* `paramBaseName[fieldBaseName]` per the OAS deepObject style, and
* Mustache provides no access to the outer scope from inside `{{#vars}}`.
*/
private void propagateParamBaseNameToVars(CodegenParameter param) {
if (!param.isQueryParam || !param.isModel || param.vars == null) {
return;
}
for (CodegenProperty v : param.vars) {
v.vendorExtensions.put("x-kotlin-param-base-name", param.baseName);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,41 @@ import com.fasterxml.jackson.databind.ObjectMapper

val localVariableQuery = mutableMapOf<String, List<String>>()
{{#queryParams}}
{{#isModel}}
{{#isDeepObject}}
{{#vars}}
{{{paramName}}}?.{{{name}}}?.let { localVariableQuery["{{vendorExtensions.x-kotlin-param-base-name}}[{{baseName}}]"] = listOf("$it") }
{{/vars}}
{{/isDeepObject}}
{{^isDeepObject}}
{{#isExplode}}
{{#vars}}
{{{paramName}}}?.{{{name}}}?.let { localVariableQuery["{{baseName}}"] = listOf("$it") }
{{/vars}}
{{/isExplode}}
{{^isExplode}}
{{{paramName}}}?.let { _model -> listOfNotNull({{#vars}}_model.{{{name}}}?.let { "{{baseName}},$it" }{{^-last}}, {{/-last}}{{/vars}}).takeIf { it.isNotEmpty() }?.let { localVariableQuery["{{baseName}}"] = listOf(it.joinToString(",")) } }
{{/isExplode}}
{{/isDeepObject}}
{{/isModel}}
{{^isModel}}
{{#isMap}}
{{#isDeepObject}}
{{{paramName}}}?.forEach { (key, value) -> localVariableQuery["{{baseName}}[$key]"] = listOf("$value") }
{{/isDeepObject}}
{{^isDeepObject}}
{{#isExplode}}
{{{paramName}}}?.forEach { (key, value) -> localVariableQuery[key] = listOf("$value") }
{{/isExplode}}
{{^isExplode}}
{{{paramName}}}?.takeIf { it.isNotEmpty() }?.let { localVariableQuery["{{baseName}}"] = listOf(it.entries.joinToString(",") { (k, v) -> "$k,$v" }) }
{{/isExplode}}
{{/isDeepObject}}
{{/isMap}}
{{^isMap}}
{{{paramName}}}?.apply { localVariableQuery["{{baseName}}"] = {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{{paramName}}}"){{/isContainer}} }
{{/isMap}}
{{/isModel}}
{{/queryParams}}

val localVariableHeaders = mutableMapOf<String, String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ public void testGeneratedApisUseExplicitDateTypeArgumentsForQuerySerialization(C
assertFileNotContains(queryApi.toPath(), "parseDateToQueryString(it)");
}

@Test
public void testJvmKtorQueryParamWithTypeObject() throws IOException {
OpenAPI openAPI = readOpenAPI("3_0/kotlin/jvm-ktor-type-object-query.yaml");

KotlinClientCodegen codegen = createCodegen(ClientLibrary.JVM_KTOR);
DefaultGenerator generator = new DefaultGenerator();
enableOnlyApiGeneration(generator);

List<File> files = generator.opts(createClientOptInput(openAPI, codegen)).generate();
File defaultApi = files.stream().filter(file -> file.getName().equals("DefaultApi.kt")).findAny().orElseThrow();

assertFileContains(defaultApi.toPath(), "mapFormExplode?.forEach { (key, value) -> localVariableQuery[key]");
assertFileContains(defaultApi.toPath(), "mapFormNoexplode?.takeIf");
assertFileContains(defaultApi.toPath(), "localVariableQuery[\"map_deep[$key]\"]");

assertFileContains(defaultApi.toPath(), "modelFormExplode?.a?.let { localVariableQuery[\"a\"]");
assertFileContains(defaultApi.toPath(), "modelFormNoexplode?.let { _model -> listOfNotNull(_model.a?.let { \"a,$it\" }, _model.b?.let { \"b,$it\" })");
assertFileContains(defaultApi.toPath(), "localVariableQuery[\"model_deep[a]\"]");

assertFileNotContains(defaultApi.toPath(), "mapDeep?.apply {");
}

private static void assertFileContainsLine(List<String> lines, String line) {
Assert.assertListContains(lines, s -> s.equals(line), line);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
openapi: 3.0.3
info:
title: jvm-ktor type:object query param coverage
version: 1.0.0
paths:
/probe:
get:
operationId: probe
parameters:
- name: scalar_form
in: query
schema:
type: string
- name: array_form_explode
in: query
style: form
explode: true
schema:
type: array
items:
type: string
- name: array_form_noexplode
in: query
style: form
explode: false
schema:
type: array
items:
type: string
- name: map_form_explode
in: query
style: form
explode: true
schema:
type: object
additionalProperties:
type: string
- name: map_form_noexplode
in: query
style: form
explode: false
schema:
type: object
additionalProperties:
type: string
- name: map_deep
in: query
style: deepObject
explode: true
schema:
type: object
additionalProperties:
type: string
- name: model_form_explode
in: query
style: form
explode: true
schema:
type: object
properties:
a:
type: string
b:
type: integer
- name: model_form_noexplode
in: query
style: form
explode: false
schema:
type: object
properties:
a:
type: string
b:
type: integer
- name: model_deep
in: query
style: deepObject
explode: true
schema:
type: object
properties:
a:
type: string
b:
type: integer
responses:
'200':
description: ok
Loading