Skip to content
Open
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 @@ -9,6 +9,7 @@
@SerializedName("{{{vendorExtensions.x-base-name-literal}}}")
{{/gson}}
{{#jackson}}
@param:JsonProperty("{{{vendorExtensions.x-base-name-literal}}}")
@get:JsonProperty("{{{vendorExtensions.x-base-name-literal}}}")
{{/jackson}}
{{#kotlinx_serialization}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@SerializedName("{{{vendorExtensions.x-base-name-literal}}}")
{{/gson}}
{{#jackson}}
@param:JsonProperty("{{{vendorExtensions.x-base-name-literal}}}")
@get:JsonProperty("{{{vendorExtensions.x-base-name-literal}}}")
{{/jackson}}
{{#kotlinx_serialization}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
@field:JsonInclude(JsonInclude.Include.NON_NULL){{/isNullable}}{{#vendorExtensions.x-has-json-setter-nulls-skip}}
@field:JsonSetter(nulls = Nulls.SKIP){{/vendorExtensions.x-has-json-setter-nulls-skip}}{{#vendorExtensions.x-has-json-setter-nulls-fail}}
@field:JsonSetter(nulls = Nulls.FAIL){{/vendorExtensions.x-has-json-setter-nulls-fail}}
@param:JsonProperty("{{{baseName}}}")
@get:JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#vendorExtensions.x-is-jackson-optional-nullable}}JsonNullable<{{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{{nameInPascalCase}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}>{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{{nameInPascalCase}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}?{{/vendorExtensions.x-is-jackson-optional-nullable}} = {{#vendorExtensions.x-is-jackson-optional-nullable}}JsonNullable.undefined(){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^defaultValue}}null{{/defaultValue}}{{#defaultValue}}{{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}{{/vendorExtensions.x-is-jackson-optional-nullable}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
@Schema({{#example}}example = "{{#lambdaRemoveLineBreak}}{{#lambdaEscapeInNormalString}}{{{.}}}{{/lambdaEscapeInNormalString}}{{/lambdaRemoveLineBreak}}", {{/example}}required = true, {{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}
@ApiModelProperty({{#example}}example = "{{#lambdaRemoveLineBreak}}{{#lambdaEscapeInNormalString}}{{{.}}}{{/lambdaEscapeInNormalString}}{{/lambdaRemoveLineBreak}}", {{/example}}required = true, {{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swagger1AnnotationLibrary}}{{#vendorExtensions.x-field-extra-annotation}}
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
@param:JsonProperty("{{{baseName}}}")
@get:JsonProperty("{{{baseName}}}", required = true){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{{nameInPascalCase}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,53 @@ public void testCompanionObjectGeneratesCompanionInModel() throws IOException {
TestUtils.assertFileContains(petModel, "companion object { }");
}

@Test(description = "nameMappings: @param:JsonProperty must use the original JSON field name for deserialization")
public void paramJsonPropertyAnnotationWithNameMappings() throws IOException {
// When a property is renamed via nameMappings, @param:JsonProperty must carry the
// original JSON field name so Jackson can deserialize from the correct JSON key.
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setInputSpec("src/test/resources/3_0/kotlin/param-json-property.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, "jackson")
.addNameMapping("snake_case_value", "mappedValue");

DefaultGenerator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();

Path itemModel = Paths.get(output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/client/models/Item.kt");
// @param:JsonProperty must reference the original JSON key, not the mapped Kotlin name
TestUtils.assertFileContains(itemModel,
"@param:JsonProperty(\"snake_case_value\")\n @get:JsonProperty(\"snake_case_value\")\n val mappedValue");
}

@Test(description = "auto-renamed digit-starting property: @param:JsonProperty must use the original JSON field name")
public void paramJsonPropertyAnnotationWithDigitStartingPropertyName() throws IOException {
// When a property name starts with a digit, the Kotlin codegen wraps it in backticks
// (e.g. "2nd_field" -> `2ndField`). @param:JsonProperty must still carry the original
// JSON field name so that Jackson can deserialize it correctly.
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setInputSpec("src/test/resources/3_0/kotlin/param-json-property.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");

DefaultGenerator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();

Path itemModel = Paths.get(output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/client/models/Item.kt");
// @param:JsonProperty must reference the original JSON key even when the property
// is auto-renamed to a backtick-escaped identifier
TestUtils.assertFileContains(itemModel,
"@param:JsonProperty(\"2nd_field\")\n @get:JsonProperty(\"2nd_field\")\n val `2ndField`");
}

private static class ModelNameTest {
private final String expectedName;
private final String expectedClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6476,7 +6476,7 @@ public void requiredNullable_scenario3_optionalNonNullable_withDefault() throws
String content = Files.readString(modelFile);
int idx = content.indexOf("val optionalNonNullableWithDefault:");
Assert.assertTrue(idx >= 0, "optionalNonNullableWithDefault property must exist");
String context = content.substring(Math.max(0, idx - 200), idx);
String context = content.substring(Math.max(0, idx - 300), idx);
Assert.assertTrue(context.contains("@field:JsonInclude(JsonInclude.Include.NON_NULL)"),
"optionalNonNullableWithDefault must have @JsonInclude(NON_NULL)");
Assert.assertTrue(context.contains("@field:JsonSetter(nulls = Nulls.SKIP)"),
Expand Down Expand Up @@ -6777,4 +6777,40 @@ public void schemaMappingWithNullableAllOfRendersNullableKotlinProperty() throws
String content = Files.readString(myObjectFile.toPath());
assertThat(content).contains("com.example.ExternalModel?");
}

@Test(description = "nameMappings: @param:JsonProperty must use the original JSON field name for deserialization")
public void paramJsonPropertyAnnotationWithNameMappings() throws IOException {
// When a property is renamed via nameMappings, @param:JsonProperty must carry the
// original JSON field name so Jackson can deserialize from the correct JSON key.
Map<String, File> files = generateFromContract(
"src/test/resources/3_0/kotlin/param-json-property.yaml",
new HashMap<>(),
new HashMap<>(),
configurator -> configurator.addNameMapping("snake_case_value", "mappedValue")
);

File itemFile = files.get("Item.kt");
assertThat(itemFile).isNotNull();
assertFileContains(
itemFile.toPath(),
"@param:JsonProperty(\"snake_case_value\")\n @get:JsonProperty(\"snake_case_value\", required = true) val mappedValue"
);
}

@Test(description = "auto-renamed digit-starting property: @param:JsonProperty must use the original JSON field name")
public void paramJsonPropertyAnnotationWithDigitStartingPropertyName() throws IOException {
// When a property name starts with a digit, the Kotlin codegen wraps it in backticks
// (e.g. "2nd_field" -> `2ndField`). @param:JsonProperty must still carry the original
// JSON field name so that Jackson can deserialize it correctly.
Map<String, File> files = generateFromContract(
"src/test/resources/3_0/kotlin/param-json-property.yaml"
);

File itemFile = files.get("Item.kt");
assertThat(itemFile).isNotNull();
assertFileContains(
itemFile.toPath(),
"@param:JsonProperty(\"2nd_field\")\n @get:JsonProperty(\"2nd_field\") val `2ndField`"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: "3.0.0"
info:
title: Test API for @param:JsonProperty annotation
version: "1.0.0"
paths:
/items:
get:
operationId: listItems
responses:
"200":
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/Item"
components:
schemas:
Item:
type: object
required:
- snake_case_value
properties:
snake_case_value:
type: string
description: A required property to be renamed via nameMappings
2nd_field:
type: string
description: An optional property whose name starts with a digit and is auto-renamed
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Bird (

@param:JsonProperty("size")
@get:JsonProperty("size")
val propertySize: kotlin.String? = null,

@param:JsonProperty("color")
@get:JsonProperty("color")
val color: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Category (

@param:JsonProperty("id")
@get:JsonProperty("id")
val id: kotlin.Long? = null,

@param:JsonProperty("name")
@get:JsonProperty("name")
val name: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,35 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class DefaultValue (

@param:JsonProperty("array_string_enum_ref_default")
@get:JsonProperty("array_string_enum_ref_default")
val arrayStringEnumRefDefault: kotlin.collections.List<StringEnumRef>? = null,

@param:JsonProperty("array_string_enum_default")
@get:JsonProperty("array_string_enum_default")
val arrayStringEnumDefault: kotlin.collections.List<DefaultValue.ArrayStringEnumDefault>? = null,

@param:JsonProperty("array_string_default")
@get:JsonProperty("array_string_default")
val arrayStringDefault: kotlin.collections.List<kotlin.String>? = arrayListOf("failure","skipped"),

@param:JsonProperty("array_integer_default")
@get:JsonProperty("array_integer_default")
val arrayIntegerDefault: kotlin.collections.List<kotlin.Int>? = arrayListOf(1,3),

@param:JsonProperty("array_string")
@get:JsonProperty("array_string")
val arrayString: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("array_string_nullable")
@get:JsonProperty("array_string_nullable")
val arrayStringNullable: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("array_string_extension_nullable")
@get:JsonProperty("array_string_extension_nullable")
val arrayStringExtensionNullable: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("string_nullable")
@get:JsonProperty("string_nullable")
val stringNullable: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class NumberPropertiesOnly (

@param:JsonProperty("number")
@get:JsonProperty("number")
val number: java.math.BigDecimal? = null,

@param:JsonProperty("float")
@get:JsonProperty("float")
val float: kotlin.Float? = null,

@param:JsonProperty("double")
@get:JsonProperty("double")
val double: kotlin.Double? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,28 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Pet (

@param:JsonProperty("name")
@get:JsonProperty("name")
val name: kotlin.String,

@param:JsonProperty("photoUrls")
@get:JsonProperty("photoUrls")
val photoUrls: kotlin.collections.List<kotlin.String>,

@param:JsonProperty("id")
@get:JsonProperty("id")
val id: kotlin.Long? = null,

@param:JsonProperty("category")
@get:JsonProperty("category")
val category: Category? = null,

@param:JsonProperty("tags")
@get:JsonProperty("tags")
val tags: kotlin.collections.List<Tag>? = null,

/* pet status in the store */
@param:JsonProperty("status")
@get:JsonProperty("status")
val status: Pet.Status? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ import com.fasterxml.jackson.annotation.JsonProperty
data class Query (

/* Query */
@param:JsonProperty("id")
@get:JsonProperty("id")
val id: kotlin.Long? = null,

@param:JsonProperty("outcomes")
@get:JsonProperty("outcomes")
val outcomes: kotlin.collections.List<Query.Outcomes>? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Tag (

@param:JsonProperty("id")
@get:JsonProperty("id")
val id: kotlin.Long? = null,

@param:JsonProperty("name")
@get:JsonProperty("name")
val name: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter (

@param:JsonProperty("values")
@get:JsonProperty("values")
val propertyValues: kotlin.collections.List<kotlin.String>? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Bird (

@param:JsonProperty("size")
@get:JsonProperty("size")
val propertySize: kotlin.String? = null,

@param:JsonProperty("color")
@get:JsonProperty("color")
val color: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class Category (

@param:JsonProperty("id")
@get:JsonProperty("id")
val id: kotlin.Long? = null,

@param:JsonProperty("name")
@get:JsonProperty("name")
val name: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,35 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class DefaultValue (

@param:JsonProperty("array_string_enum_ref_default")
@get:JsonProperty("array_string_enum_ref_default")
val arrayStringEnumRefDefault: kotlin.collections.List<StringEnumRef>? = null,

@param:JsonProperty("array_string_enum_default")
@get:JsonProperty("array_string_enum_default")
val arrayStringEnumDefault: kotlin.collections.List<DefaultValue.ArrayStringEnumDefault>? = null,

@param:JsonProperty("array_string_default")
@get:JsonProperty("array_string_default")
val arrayStringDefault: kotlin.collections.List<kotlin.String>? = arrayListOf("failure","skipped"),

@param:JsonProperty("array_integer_default")
@get:JsonProperty("array_integer_default")
val arrayIntegerDefault: kotlin.collections.List<kotlin.Int>? = arrayListOf(1,3),

@param:JsonProperty("array_string")
@get:JsonProperty("array_string")
val arrayString: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("array_string_nullable")
@get:JsonProperty("array_string_nullable")
val arrayStringNullable: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("array_string_extension_nullable")
@get:JsonProperty("array_string_extension_nullable")
val arrayStringExtensionNullable: kotlin.collections.List<kotlin.String>? = null,

@param:JsonProperty("string_nullable")
@get:JsonProperty("string_nullable")
val stringNullable: kotlin.String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class NumberPropertiesOnly (

@param:JsonProperty("number")
@get:JsonProperty("number")
val number: java.math.BigDecimal? = null,

@param:JsonProperty("float")
@get:JsonProperty("float")
val float: kotlin.Float? = null,

@param:JsonProperty("double")
@get:JsonProperty("double")
val double: kotlin.Double? = null

Expand Down
Loading
Loading