Skip to content

Commit 63c6a98

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

File tree

6 files changed

+384
-2
lines changed

6 files changed

+384
-2
lines changed

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

Lines changed: 96 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,99 @@ 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+
requireNotNull(jsonElement) {
252+
"Provided json element must not be null"
253+
}
254+
var match = 0
255+
val errorMessages = ArrayList<String>()
256+
{{#composedSchemas}}
257+
{{#anyOf}}
258+
{{^vendorExtensions.x-duplicated-data-type}}
259+
{{^hasVars}}
260+
// validate the json string with {{{dataType}}}
261+
try {
262+
// validate the JSON object to see if any exception is thrown
263+
{{^isArray}}
264+
{{#isNumber}}
265+
require(jsonElement.getAsJsonPrimitive().isNumber()) {
266+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
267+
}
268+
{{/isNumber}}
269+
{{^isNumber}}
270+
{{#isPrimitiveType}}
271+
require(jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
272+
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())
273+
}
274+
{{/isPrimitiveType}}
275+
{{/isNumber}}
276+
{{^isNumber}}
277+
{{^isPrimitiveType}}
278+
{{{dataType}}}.validateJsonElement(jsonElement)
279+
{{/isPrimitiveType}}
280+
{{/isNumber}}
281+
{{/isArray}}
282+
{{#isArray}}
283+
require(jsonElement.isJsonArray) {
284+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
285+
}
286+
287+
// validate array items
288+
for(element in jsonElement.getAsJsonArray()) {
289+
{{#items}}
290+
{{#isNumber}}
291+
require(element.getAsJsonPrimitive().isNumber) {
292+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
293+
}
294+
{{/isNumber}}
295+
{{^isNumber}}
296+
{{#isPrimitiveType}}
297+
require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
298+
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())
299+
}
300+
{{/isPrimitiveType}}
301+
{{/isNumber}}
302+
{{^isNumber}}
303+
{{^isPrimitiveType}}
304+
{{{dataType}}}.validateJsonElement(element)
305+
{{/isPrimitiveType}}
306+
{{/isNumber}}
307+
{{/items}}
308+
}
309+
{{/isArray}}
310+
match++
311+
} catch (e: Exception) {
312+
// Validation failed, continue
313+
errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
314+
}
315+
{{/hasVars}}
316+
{{#hasVars}}
317+
// validate json string for {{{.}}}
318+
try {
319+
// validate the JSON object to see if any exception is thrown
320+
{{.}}.validateJsonElement(jsonElement)
321+
match++
322+
} catch (e: Exception) {
323+
// validation failed, continue
324+
errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
325+
}
326+
{{/hasVars}}
327+
{{/vendorExtensions.x-duplicated-data-type}}
328+
{{/anyOf}}
329+
{{/composedSchemas}}
330+
331+
if (match != 1) {
332+
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()))
333+
}
334+
}
335+
}
241336
}

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

Lines changed: 96 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,99 @@ 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+
requireNotNull(jsonElement) {
250+
"Provided json element must not be null"
251+
}
252+
var match = 0
253+
val errorMessages = ArrayList<String>()
254+
{{#composedSchemas}}
255+
{{#oneOf}}
256+
{{^vendorExtensions.x-duplicated-data-type}}
257+
{{^hasVars}}
258+
// validate the json string with {{{dataType}}}
259+
try {
260+
// validate the JSON object to see if any exception is thrown
261+
{{^isArray}}
262+
{{#isNumber}}
263+
require(jsonElement.getAsJsonPrimitive().isNumber()) {
264+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
265+
}
266+
{{/isNumber}}
267+
{{^isNumber}}
268+
{{#isPrimitiveType}}
269+
require(jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
270+
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())
271+
}
272+
{{/isPrimitiveType}}
273+
{{/isNumber}}
274+
{{^isNumber}}
275+
{{^isPrimitiveType}}
276+
{{{dataType}}}.validateJsonElement(jsonElement)
277+
{{/isPrimitiveType}}
278+
{{/isNumber}}
279+
{{/isArray}}
280+
{{#isArray}}
281+
require(jsonElement.isJsonArray) {
282+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
283+
}
284+
285+
// validate array items
286+
for(element in jsonElement.getAsJsonArray()) {
287+
{{#items}}
288+
{{#isNumber}}
289+
require(jsonElement.getAsJsonPrimitive().isNumber) {
290+
String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
291+
}
292+
{{/isNumber}}
293+
{{^isNumber}}
294+
{{#isPrimitiveType}}
295+
require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
296+
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())
297+
}
298+
{{/isPrimitiveType}}
299+
{{/isNumber}}
300+
{{^isNumber}}
301+
{{^isPrimitiveType}}
302+
{{{dataType}}}.validateJsonElement(element)
303+
{{/isPrimitiveType}}
304+
{{/isNumber}}
305+
{{/items}}
306+
}
307+
{{/isArray}}
308+
match++
309+
} catch (e: Exception) {
310+
// Validation failed, continue
311+
errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
312+
}
313+
{{/hasVars}}
314+
{{#hasVars}}
315+
// validate json string for {{{.}}}
316+
try {
317+
// validate the JSON object to see if any exception is thrown
318+
{{.}}.validateJsonElement(jsonElement)
319+
match++
320+
} catch (e: Exception) {
321+
// validation failed, continue
322+
errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
323+
}
324+
{{/hasVars}}
325+
{{/vendorExtensions.x-duplicated-data-type}}
326+
{{/oneOf}}
327+
{{/composedSchemas}}
328+
329+
if (match != 1) {
330+
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()))
331+
}
332+
}
333+
}
239334
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,43 @@ 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+
requireNotNull(jsonElement) {
125+
"Provided json element must not be null"
126+
}
127+
var match = 0
128+
val errorMessages = ArrayList<String>()
129+
// validate the json string with ApiUser
130+
try {
131+
// validate the JSON object to see if any exception is thrown
132+
ApiUser.validateJsonElement(jsonElement)
133+
match++
134+
} catch (e: Exception) {
135+
// Validation failed, continue
136+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
137+
}
138+
// validate the json string with ApiPet
139+
try {
140+
// validate the JSON object to see if any exception is thrown
141+
ApiPet.validateJsonElement(jsonElement)
142+
match++
143+
} catch (e: Exception) {
144+
// Validation failed, continue
145+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
146+
}
147+
148+
if (match != 1) {
149+
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()))
150+
}
151+
}
152+
}
114153
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,61 @@ 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+
requireNotNull(jsonElement) {
158+
"Provided json element must not be null"
159+
}
160+
var match = 0
161+
val errorMessages = ArrayList<String>()
162+
// validate the json string with ApiUser
163+
try {
164+
// validate the JSON object to see if any exception is thrown
165+
ApiUser.validateJsonElement(jsonElement)
166+
match++
167+
} catch (e: Exception) {
168+
// Validation failed, continue
169+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
170+
}
171+
// validate the json string with ApiPet
172+
try {
173+
// validate the JSON object to see if any exception is thrown
174+
ApiPet.validateJsonElement(jsonElement)
175+
match++
176+
} catch (e: Exception) {
177+
// Validation failed, continue
178+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
179+
}
180+
// validate the json string with kotlin.collections.List<kotlin.String>
181+
try {
182+
// validate the JSON object to see if any exception is thrown
183+
require(jsonElement.isJsonArray) {
184+
String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
185+
}
186+
187+
// validate array items
188+
for(element in jsonElement.getAsJsonArray()) {
189+
require(element.getAsJsonPrimitive().isString) {
190+
String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString())
191+
}
192+
}
193+
match++
194+
} catch (e: Exception) {
195+
// Validation failed, continue
196+
errorMessages.add(String.format("Validation for kotlin.collections.List<kotlin.String> failed with `%s`.", e.message))
197+
}
198+
199+
if (match != 1) {
200+
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()))
201+
}
202+
}
203+
}
147204
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,43 @@ 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+
requireNotNull(jsonElement) {
129+
"Provided json element must not be null"
130+
}
131+
var match = 0
132+
val errorMessages = ArrayList<String>()
133+
// validate the json string with ApiUser
134+
try {
135+
// validate the JSON object to see if any exception is thrown
136+
ApiUser.validateJsonElement(jsonElement)
137+
match++
138+
} catch (e: Exception) {
139+
// Validation failed, continue
140+
errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
141+
}
142+
// validate the json string with ApiPet
143+
try {
144+
// validate the JSON object to see if any exception is thrown
145+
ApiPet.validateJsonElement(jsonElement)
146+
match++
147+
} catch (e: Exception) {
148+
// Validation failed, continue
149+
errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
150+
}
151+
152+
if (match != 1) {
153+
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()))
154+
}
155+
}
156+
}
118157
}

0 commit comments

Comments
 (0)