diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache index 9804f8c261c9..56786362a309 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache @@ -185,7 +185,7 @@ import java.io.IOException for(element in jsonElement.getAsJsonArray()) { {{#items}} {{#isNumber}} - require(jsonElement.getAsJsonPrimitive().isNumber) { + require(element.getAsJsonPrimitive().isNumber) { String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) } {{/isNumber}} @@ -238,4 +238,96 @@ import java.io.IOException }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to {{classname}} + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + {{#composedSchemas}} + {{#anyOf}} + {{^vendorExtensions.x-duplicated-data-type}} + {{^hasVars}} + // validate the json string with {{{dataType}}} + try { + // validate the JSON object to see if any exception is thrown + {{^isArray}} + {{#isNumber}} + require(jsonElement!!.getAsJsonPrimitive().isNumber()) { + String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) + } + {{/isNumber}} + {{^isNumber}} + {{#isPrimitiveType}} + require(jsonElement!!.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) { + 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()) + } + {{/isPrimitiveType}} + {{/isNumber}} + {{^isNumber}} + {{^isPrimitiveType}} + {{{dataType}}}.validateJsonElement(jsonElement) + {{/isPrimitiveType}} + {{/isNumber}} + {{/isArray}} + {{#isArray}} + require(jsonElement!!.isJsonArray) { + String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()) + } + + // validate array items + for(element in jsonElement.getAsJsonArray()) { + {{#items}} + {{#isNumber}} + require(element.getAsJsonPrimitive().isNumber) { + String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) + } + {{/isNumber}} + {{^isNumber}} + {{#isPrimitiveType}} + require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) { + 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()) + } + {{/isPrimitiveType}} + {{/isNumber}} + {{^isNumber}} + {{^isPrimitiveType}} + {{{dataType}}}.validateJsonElement(element) + {{/isPrimitiveType}} + {{/isNumber}} + {{/items}} + } + {{/isArray}} + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message)) + } + {{/hasVars}} + {{#hasVars}} + // validate json string for {{{.}}} + try { + // validate the JSON object to see if any exception is thrown + {{.}}.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // validation failed, continue + errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message)) + } + {{/hasVars}} + {{/vendorExtensions.x-duplicated-data-type}} + {{/anyOf}} + {{/composedSchemas}} + + if (match != 1) { + 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())) + } + } + } } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache index ae6cff2298b7..a1e71c548272 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache @@ -179,7 +179,7 @@ import java.io.IOException for(element in jsonElement.getAsJsonArray()) { {{#items}} {{#isNumber}} - require(jsonElement.getAsJsonPrimitive().isNumber) { + require(element.getAsJsonPrimitive().isNumber) { String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) } {{/isNumber}} @@ -236,4 +236,96 @@ import java.io.IOException }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to {{classname}} + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + {{#composedSchemas}} + {{#oneOf}} + {{^vendorExtensions.x-duplicated-data-type}} + {{^hasVars}} + // validate the json string with {{{dataType}}} + try { + // validate the JSON object to see if any exception is thrown + {{^isArray}} + {{#isNumber}} + require(jsonElement!!.getAsJsonPrimitive().isNumber()) { + String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) + } + {{/isNumber}} + {{^isNumber}} + {{#isPrimitiveType}} + require(jsonElement!!.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) { + 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()) + } + {{/isPrimitiveType}} + {{/isNumber}} + {{^isNumber}} + {{^isPrimitiveType}} + {{{dataType}}}.validateJsonElement(jsonElement) + {{/isPrimitiveType}} + {{/isNumber}} + {{/isArray}} + {{#isArray}} + require(jsonElement!!.isJsonArray) { + String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()) + } + + // validate array items + for(element in jsonElement!!.getAsJsonArray()) { + {{#items}} + {{#isNumber}} + require(jsonElement!!.getAsJsonPrimitive().isNumber) { + String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString()) + } + {{/isNumber}} + {{^isNumber}} + {{#isPrimitiveType}} + require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) { + 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()) + } + {{/isPrimitiveType}} + {{/isNumber}} + {{^isNumber}} + {{^isPrimitiveType}} + {{{dataType}}}.validateJsonElement(element) + {{/isPrimitiveType}} + {{/isNumber}} + {{/items}} + } + {{/isArray}} + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message)) + } + {{/hasVars}} + {{#hasVars}} + // validate json string for {{{.}}} + try { + // validate the JSON object to see if any exception is thrown + {{.}}.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // validation failed, continue + errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message)) + } + {{/hasVars}} + {{/vendorExtensions.x-duplicated-data-type}} + {{/oneOf}} + {{/composedSchemas}} + + if (match != 1) { + 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())) + } + } + } } \ No newline at end of file diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt index 3d523a450eac..2e93ea377168 100644 --- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt +++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt @@ -111,4 +111,40 @@ data class ApiAnyOfUserOrPet(var actualInstance: Any? = null) { }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPet + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + // validate the json string with ApiUser + try { + // validate the JSON object to see if any exception is thrown + ApiUser.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message)) + } + // validate the json string with ApiPet + try { + // validate the JSON object to see if any exception is thrown + ApiPet.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message)) + } + + if (match != 1) { + 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())) + } + } + } } diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt index 1ec7549f45c0..976630c764e1 100644 --- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt +++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt @@ -144,4 +144,58 @@ data class ApiAnyOfUserOrPetOrArrayString(var actualInstance: Any? = null) { }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPetOrArrayString + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + // validate the json string with ApiUser + try { + // validate the JSON object to see if any exception is thrown + ApiUser.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message)) + } + // validate the json string with ApiPet + try { + // validate the JSON object to see if any exception is thrown + ApiPet.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message)) + } + // validate the json string with kotlin.collections.List + try { + // validate the JSON object to see if any exception is thrown + require(jsonElement!!.isJsonArray) { + String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()) + } + + // validate array items + for(element in jsonElement.getAsJsonArray()) { + require(element.getAsJsonPrimitive().isString) { + String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()) + } + } + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for kotlin.collections.List failed with `%s`.", e.message)) + } + + if (match != 1) { + 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())) + } + } + } } diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt index 372906b0e56b..20db8006347b 100644 --- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt +++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt @@ -115,4 +115,40 @@ data class ApiUserOrPet(var actualInstance: Any? = null) { }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to ApiUserOrPet + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + // validate the json string with ApiUser + try { + // validate the JSON object to see if any exception is thrown + ApiUser.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message)) + } + // validate the json string with ApiPet + try { + // validate the JSON object to see if any exception is thrown + ApiPet.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message)) + } + + if (match != 1) { + 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())) + } + } + } } diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt index 009b3bef5230..9a5b06b2a9a5 100644 --- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt +++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt @@ -147,4 +147,58 @@ data class ApiUserOrPetOrArrayString(var actualInstance: Any? = null) { }.nullSafe() as TypeAdapter } } + + companion object { + /** + * Validates the JSON Element and throws an exception if issues found + * + * @param jsonElement JSON Element + * @throws IOException if the JSON Element is invalid with respect to ApiUserOrPetOrArrayString + */ + @Throws(IOException::class) + fun validateJsonElement(jsonElement: JsonElement?) { + var match = 0 + val errorMessages = ArrayList() + // validate the json string with ApiUser + try { + // validate the JSON object to see if any exception is thrown + ApiUser.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message)) + } + // validate the json string with ApiPet + try { + // validate the JSON object to see if any exception is thrown + ApiPet.validateJsonElement(jsonElement) + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message)) + } + // validate the json string with kotlin.collections.List + try { + // validate the JSON object to see if any exception is thrown + require(jsonElement!!.isJsonArray) { + String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString()) + } + + // validate array items + for(element in jsonElement!!.getAsJsonArray()) { + require(element.getAsJsonPrimitive().isString) { + String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString()) + } + } + match++ + } catch (e: Exception) { + // Validation failed, continue + errorMessages.add(String.format("Validation for kotlin.collections.List failed with `%s`.", e.message)) + } + + if (match != 1) { + throw IOException(String.format("Failed validation for ApiUserOrPetOrArrayString: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString())) + } + } + } }