Skip to content

Commit 54c48e0

Browse files
committed
[kotlin] add missing validateJsonElement method to oneOf and anyOf model templates (#19942)
1 parent 67e9a53 commit 54c48e0

File tree

6 files changed

+366
-2
lines changed

6 files changed

+366
-2
lines changed

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

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ import java.io.IOException
185185
for(element in jsonElement.getAsJsonArray()) {
186186
{{#items}}
187187
{{#isNumber}}
188-
require(jsonElement.getAsJsonPrimitive().isNumber) {
188+
require(element.getAsJsonPrimitive().isNumber) {
189189
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
190190
}
191191
{{/isNumber}}
@@ -238,4 +238,96 @@ import java.io.IOException
238238
}.nullSafe() as TypeAdapter<T>
239239
}
240240
}
241+
242+
companion object {
243+
/**
244+
* Validates the JSON Element and throws an exception if issues found
245+
*
246+
* @param jsonElement JSON Element
247+
* @throws IOException if the JSON Element is invalid with respect to {{classname}}
248+
*/
249+
@Throws(IOException::class)
250+
fun validateJsonElement(jsonElement: JsonElement?) {
251+
var match = 0
252+
val errorMessages = ArrayList<String>()
253+
{{#composedSchemas}}
254+
{{#anyOf}}
255+
{{^vendorExtensions.x-duplicated-data-type}}
256+
{{^hasVars}}
257+
// validate the json string with {{{dataType}}}
258+
try {
259+
// validate the JSON object to see if any exception is thrown
260+
{{^isArray}}
261+
{{#isNumber}}
262+
require(jsonElement!!.getAsJsonPrimitive().isNumber()) {
263+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
264+
}
265+
{{/isNumber}}
266+
{{^isNumber}}
267+
{{#isPrimitiveType}}
268+
require(jsonElement!!.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
269+
String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
270+
}
271+
{{/isPrimitiveType}}
272+
{{/isNumber}}
273+
{{^isNumber}}
274+
{{^isPrimitiveType}}
275+
{{{dataType}}}.validateJsonElement(jsonElement)
276+
{{/isPrimitiveType}}
277+
{{/isNumber}}
278+
{{/isArray}}
279+
{{#isArray}}
280+
require(jsonElement!!.isJsonArray) {
281+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
282+
}
283+
284+
// validate array items
285+
for(element in jsonElement.getAsJsonArray()) {
286+
{{#items}}
287+
{{#isNumber}}
288+
require(element.getAsJsonPrimitive().isNumber) {
289+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
290+
}
291+
{{/isNumber}}
292+
{{^isNumber}}
293+
{{#isPrimitiveType}}
294+
require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
295+
String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
296+
}
297+
{{/isPrimitiveType}}
298+
{{/isNumber}}
299+
{{^isNumber}}
300+
{{^isPrimitiveType}}
301+
{{{dataType}}}.validateJsonElement(element)
302+
{{/isPrimitiveType}}
303+
{{/isNumber}}
304+
{{/items}}
305+
}
306+
{{/isArray}}
307+
match++
308+
} catch (e: Exception) {
309+
// Validation failed, continue
310+
errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
311+
}
312+
{{/hasVars}}
313+
{{#hasVars}}
314+
// validate json string for {{{.}}}
315+
try {
316+
// validate the JSON object to see if any exception is thrown
317+
{{.}}.validateJsonElement(jsonElement)
318+
match++
319+
} catch (e: Exception) {
320+
// validation failed, continue
321+
errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
322+
}
323+
{{/hasVars}}
324+
{{/vendorExtensions.x-duplicated-data-type}}
325+
{{/anyOf}}
326+
{{/composedSchemas}}
327+
328+
if (match != 1) {
329+
throw IOException(String.format("Failed validation for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
330+
}
331+
}
332+
}
241333
}

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

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ import java.io.IOException
179179
for(element in jsonElement.getAsJsonArray()) {
180180
{{#items}}
181181
{{#isNumber}}
182-
require(jsonElement.getAsJsonPrimitive().isNumber) {
182+
require(element.getAsJsonPrimitive().isNumber) {
183183
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
184184
}
185185
{{/isNumber}}
@@ -236,4 +236,96 @@ import java.io.IOException
236236
}.nullSafe() as TypeAdapter<T>
237237
}
238238
}
239+
240+
companion object {
241+
/**
242+
* Validates the JSON Element and throws an exception if issues found
243+
*
244+
* @param jsonElement JSON Element
245+
* @throws IOException if the JSON Element is invalid with respect to {{classname}}
246+
*/
247+
@Throws(IOException::class)
248+
fun validateJsonElement(jsonElement: JsonElement?) {
249+
var match = 0
250+
val errorMessages = ArrayList<String>()
251+
{{#composedSchemas}}
252+
{{#oneOf}}
253+
{{^vendorExtensions.x-duplicated-data-type}}
254+
{{^hasVars}}
255+
// validate the json string with {{{dataType}}}
256+
try {
257+
// validate the JSON object to see if any exception is thrown
258+
{{^isArray}}
259+
{{#isNumber}}
260+
require(jsonElement!!.getAsJsonPrimitive().isNumber()) {
261+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
262+
}
263+
{{/isNumber}}
264+
{{^isNumber}}
265+
{{#isPrimitiveType}}
266+
require(jsonElement!!.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
267+
String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
268+
}
269+
{{/isPrimitiveType}}
270+
{{/isNumber}}
271+
{{^isNumber}}
272+
{{^isPrimitiveType}}
273+
{{{dataType}}}.validateJsonElement(jsonElement)
274+
{{/isPrimitiveType}}
275+
{{/isNumber}}
276+
{{/isArray}}
277+
{{#isArray}}
278+
require(jsonElement!!.isJsonArray) {
279+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
280+
}
281+
282+
// validate array items
283+
for(element in jsonElement!!.getAsJsonArray()) {
284+
{{#items}}
285+
{{#isNumber}}
286+
require(jsonElement!!.getAsJsonPrimitive().isNumber) {
287+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
288+
}
289+
{{/isNumber}}
290+
{{^isNumber}}
291+
{{#isPrimitiveType}}
292+
require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
293+
String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
294+
}
295+
{{/isPrimitiveType}}
296+
{{/isNumber}}
297+
{{^isNumber}}
298+
{{^isPrimitiveType}}
299+
{{{dataType}}}.validateJsonElement(element)
300+
{{/isPrimitiveType}}
301+
{{/isNumber}}
302+
{{/items}}
303+
}
304+
{{/isArray}}
305+
match++
306+
} catch (e: Exception) {
307+
// Validation failed, continue
308+
errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
309+
}
310+
{{/hasVars}}
311+
{{#hasVars}}
312+
// validate json string for {{{.}}}
313+
try {
314+
// validate the JSON object to see if any exception is thrown
315+
{{.}}.validateJsonElement(jsonElement)
316+
match++
317+
} catch (e: Exception) {
318+
// validation failed, continue
319+
errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
320+
}
321+
{{/hasVars}}
322+
{{/vendorExtensions.x-duplicated-data-type}}
323+
{{/oneOf}}
324+
{{/composedSchemas}}
325+
326+
if (match != 1) {
327+
throw IOException(String.format("Failed validation for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
328+
}
329+
}
330+
}
239331
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,40 @@ data class ApiAnyOfUserOrPet(var actualInstance: Any? = null) {
111111
}.nullSafe() as TypeAdapter<T>
112112
}
113113
}
114+
115+
companion object {
116+
/**
117+
* Validates the JSON Element and throws an exception if issues found
118+
*
119+
* @param jsonElement JSON Element
120+
* @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPet
121+
*/
122+
@Throws(IOException::class)
123+
fun validateJsonElement(jsonElement: JsonElement?) {
124+
var match = 0
125+
val errorMessages = ArrayList<String>()
126+
// validate the json string with ApiUser
127+
try {
128+
// validate the JSON object to see if any exception is thrown
129+
ApiUser.validateJsonElement(jsonElement)
130+
match++
131+
} catch (e: Exception) {
132+
// Validation failed, continue
133+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
134+
}
135+
// validate the json string with ApiPet
136+
try {
137+
// validate the JSON object to see if any exception is thrown
138+
ApiPet.validateJsonElement(jsonElement)
139+
match++
140+
} catch (e: Exception) {
141+
// Validation failed, continue
142+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
143+
}
144+
145+
if (match != 1) {
146+
throw IOException(String.format("Failed validation for ApiAnyOfUserOrPet: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
147+
}
148+
}
149+
}
114150
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,58 @@ data class ApiAnyOfUserOrPetOrArrayString(var actualInstance: Any? = null) {
144144
}.nullSafe() as TypeAdapter<T>
145145
}
146146
}
147+
148+
companion object {
149+
/**
150+
* Validates the JSON Element and throws an exception if issues found
151+
*
152+
* @param jsonElement JSON Element
153+
* @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPetOrArrayString
154+
*/
155+
@Throws(IOException::class)
156+
fun validateJsonElement(jsonElement: JsonElement?) {
157+
var match = 0
158+
val errorMessages = ArrayList<String>()
159+
// validate the json string with ApiUser
160+
try {
161+
// validate the JSON object to see if any exception is thrown
162+
ApiUser.validateJsonElement(jsonElement)
163+
match++
164+
} catch (e: Exception) {
165+
// Validation failed, continue
166+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
167+
}
168+
// validate the json string with ApiPet
169+
try {
170+
// validate the JSON object to see if any exception is thrown
171+
ApiPet.validateJsonElement(jsonElement)
172+
match++
173+
} catch (e: Exception) {
174+
// Validation failed, continue
175+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
176+
}
177+
// validate the json string with kotlin.collections.List<kotlin.String>
178+
try {
179+
// validate the JSON object to see if any exception is thrown
180+
require(jsonElement!!.isJsonArray) {
181+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
182+
}
183+
184+
// validate array items
185+
for(element in jsonElement.getAsJsonArray()) {
186+
require(element.getAsJsonPrimitive().isString) {
187+
String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString())
188+
}
189+
}
190+
match++
191+
} catch (e: Exception) {
192+
// Validation failed, continue
193+
errorMessages.add(String.format("Validation for kotlin.collections.List<kotlin.String> failed with `%s`.", e.message))
194+
}
195+
196+
if (match != 1) {
197+
throw IOException(String.format("Failed validation for ApiAnyOfUserOrPetOrArrayString: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
198+
}
199+
}
200+
}
147201
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,40 @@ data class ApiUserOrPet(var actualInstance: Any? = null) {
115115
}.nullSafe() as TypeAdapter<T>
116116
}
117117
}
118+
119+
companion object {
120+
/**
121+
* Validates the JSON Element and throws an exception if issues found
122+
*
123+
* @param jsonElement JSON Element
124+
* @throws IOException if the JSON Element is invalid with respect to ApiUserOrPet
125+
*/
126+
@Throws(IOException::class)
127+
fun validateJsonElement(jsonElement: JsonElement?) {
128+
var match = 0
129+
val errorMessages = ArrayList<String>()
130+
// validate the json string with ApiUser
131+
try {
132+
// validate the JSON object to see if any exception is thrown
133+
ApiUser.validateJsonElement(jsonElement)
134+
match++
135+
} catch (e: Exception) {
136+
// Validation failed, continue
137+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
138+
}
139+
// validate the json string with ApiPet
140+
try {
141+
// validate the JSON object to see if any exception is thrown
142+
ApiPet.validateJsonElement(jsonElement)
143+
match++
144+
} catch (e: Exception) {
145+
// Validation failed, continue
146+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
147+
}
148+
149+
if (match != 1) {
150+
throw IOException(String.format("Failed validation for ApiUserOrPet: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
151+
}
152+
}
153+
}
118154
}

0 commit comments

Comments
 (0)