Better error check if discriminator value is null in r client (#13930)

* better error check if discriminator value is null in r client

* more readable error messages
This commit is contained in:
William Cheng
2022-11-07 02:16:16 +08:00
committed by GitHub
parent b35ea31e82
commit 363906fda3
20 changed files with 86 additions and 64 deletions

View File

@@ -85,8 +85,8 @@
{{/isNull}}
{{/composedSchemas.anyOf}}
# no match
stop(paste("No match found when deserializing the payload into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: ",
paste(error_messages, collapse = ", ")))
stop(paste("No match found when deserializing the input into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: >>",
paste(error_messages, collapse = " >> ")))
},
#' Serialize {{{classname}}} to JSON string.
#'

View File

@@ -65,6 +65,9 @@
{{#discriminator}}
oneof_lookup_result <- tryCatch({
discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`{{{propertyBaseName}}}`
if (is.null(discriminatorValue)) { # throw error if it's null
stop("Error! The value of the discriminator property `{{{propertyBaseName}}}`, which should be the class type, is null")
}
switch(discriminatorValue,
{{#mappedModels}}
{{{mappingName}}}={
@@ -89,7 +92,7 @@
error = function(err) err
)
if (!is.null(oneof_lookup_result["error"])) {
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. Input: %s", oneof_lookup_result["message"], input))
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. JSON input: %s", oneof_lookup_result["message"], input))
}
{{/discriminator}}
@@ -127,11 +130,12 @@
self$actual_type <- instance_type
} else if (matched > 1) {
# more than 1 match
stop("Multiple matches found when deserializing the payload into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}.")
stop(paste("Multiple matches found when deserializing the input into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Matched schemas: ",
paste(matched_schemas, collapse = ", ")))
} else {
# no match
stop(paste("No match found when deserializing the payload into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: ",
paste(error_messages, collapse = ", ")))
stop(paste("No match found when deserializing the input into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: >>",
paste(error_messages, collapse = " >> ")))
}
self