Skip to content

Commit 05e672d

Browse files
[Kotlin] Primitive array items validity check (OpenAPITools#21315)
* Primitive array items validity check * Update samples
1 parent 2c67841 commit 05e672d

File tree

2 files changed

+18
-0
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client
  • samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models

2 files changed

+18
-0
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ import {{packageName}}.infrastructure.ITransformForStorage
291291
String.format("Expected the field `{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}` to be an array in the JSON string but got `%s`", jsonObj["{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}"].toString())
292292
}
293293
{{/required}}
294+
{{#items.isPrimitiveType}}
295+
// ensure the items in json array are primitive
296+
if (jsonObj["{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}"] != null) {
297+
for (i in 0 until jsonObj.getAsJsonArray("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}").size()) {
298+
require(jsonObj.getAsJsonArray("{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}").get(i).isJsonPrimitive) {
299+
String.format("Expected the property in array `{{#lambda.escapeDollar}}{{baseName}}{{/lambda.escapeDollar}}` to be primitive")
300+
}
301+
}
302+
}
303+
{{/items.isPrimitiveType}}
294304
{{/items.isModel}}
295305
{{/isArray}}
296306
{{^isContainer}}

samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiPet.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ data class ApiPet (
151151
require(jsonObj["photoUrls"].isJsonArray()) {
152152
String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj["photoUrls"].toString())
153153
}
154+
// ensure the items in json array are primitive
155+
if (jsonObj["photoUrls"] != null) {
156+
for (i in 0 until jsonObj.getAsJsonArray("photoUrls").size()) {
157+
require(jsonObj.getAsJsonArray("photoUrls").get(i).isJsonPrimitive) {
158+
String.format("Expected the property in array `photoUrls` to be primitive")
159+
}
160+
}
161+
}
154162
// validate the optional field `category`
155163
if (jsonObj["category"] != null && !jsonObj["category"].isJsonNull) {
156164
ApiCategory.validateJsonElement(jsonObj["category"])

0 commit comments

Comments
 (0)