Skip to content

Commit

Permalink
[kotlin] add missing validateJsonElement method to oneOf and anyOf mo…
Browse files Browse the repository at this point in the history
…del templates (#19942)
  • Loading branch information
CaptainAye committed Oct 26, 2024
1 parent 67e9a53 commit 54c48e0
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -238,4 +238,96 @@ import java.io.IOException
}.nullSafe() as TypeAdapter<T>
}
}

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<String>()
{{#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()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -236,4 +236,96 @@ import java.io.IOException
}.nullSafe() as TypeAdapter<T>
}
}

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<String>()
{{#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()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,40 @@ data class ApiAnyOfUserOrPet(var actualInstance: Any? = null) {
}.nullSafe() as TypeAdapter<T>
}
}

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<String>()
// 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()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,58 @@ data class ApiAnyOfUserOrPetOrArrayString(var actualInstance: Any? = null) {
}.nullSafe() as TypeAdapter<T>
}
}

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<String>()
// 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<kotlin.String>
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<kotlin.String> 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()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,40 @@ data class ApiUserOrPet(var actualInstance: Any? = null) {
}.nullSafe() as TypeAdapter<T>
}
}

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<String>()
// 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()))
}
}
}
}
Loading

0 comments on commit 54c48e0

Please sign in to comment.