Skip to content

Commit 363906f

Browse files
authored
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
1 parent b35ea31 commit 363906f

20 files changed

+86
-64
lines changed

modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
{{/isNull}}
8686
{{/composedSchemas.anyOf}}
8787
# no match
88-
stop(paste("No match found when deserializing the payload into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: ",
89-
paste(error_messages, collapse = ", ")))
88+
stop(paste("No match found when deserializing the input into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: >>",
89+
paste(error_messages, collapse = " >> ")))
9090
},
9191
#' Serialize {{{classname}}} to JSON string.
9292
#'

modules/openapi-generator/src/main/resources/r/modelOneOf.mustache

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
{{#discriminator}}
6666
oneof_lookup_result <- tryCatch({
6767
discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`{{{propertyBaseName}}}`
68+
if (is.null(discriminatorValue)) { # throw error if it's null
69+
stop("Error! The value of the discriminator property `{{{propertyBaseName}}}`, which should be the class type, is null")
70+
}
6871
switch(discriminatorValue,
6972
{{#mappedModels}}
7073
{{{mappingName}}}={
@@ -89,7 +92,7 @@
8992
error = function(err) err
9093
)
9194
if (!is.null(oneof_lookup_result["error"])) {
92-
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. Input: %s", oneof_lookup_result["message"], input))
95+
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. JSON input: %s", oneof_lookup_result["message"], input))
9396
}
9497
9598
{{/discriminator}}
@@ -127,11 +130,12 @@
127130
self$actual_type <- instance_type
128131
} else if (matched > 1) {
129132
# more than 1 match
130-
stop("Multiple matches found when deserializing the payload into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}.")
133+
stop(paste("Multiple matches found when deserializing the input into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Matched schemas: ",
134+
paste(matched_schemas, collapse = ", ")))
131135
} else {
132136
# no match
133-
stop(paste("No match found when deserializing the payload into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: ",
134-
paste(error_messages, collapse = ", ")))
137+
stop(paste("No match found when deserializing the input into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: >>",
138+
paste(error_messages, collapse = " >> ")))
135139
}
136140
137141
self

samples/client/petstore/R-httr2-wrapper/R/any_of_pig.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ AnyOfPig <- R6::R6Class(
9090
}
9191

9292
# no match
93-
stop(paste("No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: ",
94-
paste(error_messages, collapse = ", ")))
93+
stop(paste("No match found when deserializing the input into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: >>",
94+
paste(error_messages, collapse = " >> ")))
9595
},
9696
#' Serialize AnyOfPig to JSON string.
9797
#'

samples/client/petstore/R-httr2-wrapper/R/any_of_primitive_type_test.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ AnyOfPrimitiveTypeTest <- R6::R6Class(
102102
self$actual_type <- instance_type
103103
} else if (matched > 1) {
104104
# more than 1 match
105-
stop("Multiple matches found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer.")
105+
stop(paste("Multiple matches found when deserializing the input into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Matched schemas: ",
106+
paste(matched_schemas, collapse = ", ")))
106107
} else {
107108
# no match
108-
stop(paste("No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ",
109-
paste(error_messages, collapse = ", ")))
109+
stop(paste("No match found when deserializing the input into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: >>",
110+
paste(error_messages, collapse = " >> ")))
110111
}
111112

112113
self

samples/client/petstore/R-httr2-wrapper/R/mammal.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Mammal <- R6::R6Class(
6666

6767
oneof_lookup_result <- tryCatch({
6868
discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`className`
69+
if (is.null(discriminatorValue)) { # throw error if it's null
70+
stop("Error! The value of the discriminator property `className`, which should be the class type, is null")
71+
}
6972
switch(discriminatorValue,
7073
whale={
7174
Whale$public_methods$validateJSON(input)
@@ -84,7 +87,7 @@ Mammal <- R6::R6Class(
8487
error = function(err) err
8588
)
8689
if (!is.null(oneof_lookup_result["error"])) {
87-
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Mammal. Error message: %s. Input: %s", oneof_lookup_result["message"], input))
90+
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Mammal. Error message: %s. JSON input: %s", oneof_lookup_result["message"], input))
8891
}
8992

9093
Whale_result <- tryCatch({
@@ -123,11 +126,12 @@ Mammal <- R6::R6Class(
123126
self$actual_type <- instance_type
124127
} else if (matched > 1) {
125128
# more than 1 match
126-
stop("Multiple matches found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra.")
129+
stop(paste("Multiple matches found when deserializing the input into Mammal with oneOf schemas Whale, Zebra. Matched schemas: ",
130+
paste(matched_schemas, collapse = ", ")))
127131
} else {
128132
# no match
129-
stop(paste("No match found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra. Details: ",
130-
paste(error_messages, collapse = ", ")))
133+
stop(paste("No match found when deserializing the input into Mammal with oneOf schemas Whale, Zebra. Details: >>",
134+
paste(error_messages, collapse = " >> ")))
131135
}
132136

133137
self

samples/client/petstore/R-httr2-wrapper/R/one_of_primitive_type_test.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ OneOfPrimitiveTypeTest <- R6::R6Class(
102102
self$actual_type <- instance_type
103103
} else if (matched > 1) {
104104
# more than 1 match
105-
stop("Multiple matches found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer.")
105+
stop(paste("Multiple matches found when deserializing the input into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Matched schemas: ",
106+
paste(matched_schemas, collapse = ", ")))
106107
} else {
107108
# no match
108-
stop(paste("No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ",
109-
paste(error_messages, collapse = ", ")))
109+
stop(paste("No match found when deserializing the input into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: >>",
110+
paste(error_messages, collapse = " >> ")))
110111
}
111112

112113
self

samples/client/petstore/R-httr2-wrapper/R/pig.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Pig <- R6::R6Class(
6666

6767
oneof_lookup_result <- tryCatch({
6868
discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`className`
69+
if (is.null(discriminatorValue)) { # throw error if it's null
70+
stop("Error! The value of the discriminator property `className`, which should be the class type, is null")
71+
}
6972
switch(discriminatorValue,
7073
BasquePig={
7174
BasquePig$public_methods$validateJSON(input)
@@ -84,7 +87,7 @@ Pig <- R6::R6Class(
8487
error = function(err) err
8588
)
8689
if (!is.null(oneof_lookup_result["error"])) {
87-
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Pig. Error message: %s. Input: %s", oneof_lookup_result["message"], input))
90+
error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Pig. Error message: %s. JSON input: %s", oneof_lookup_result["message"], input))
8891
}
8992

9093
BasquePig_result <- tryCatch({
@@ -123,11 +126,12 @@ Pig <- R6::R6Class(
123126
self$actual_type <- instance_type
124127
} else if (matched > 1) {
125128
# more than 1 match
126-
stop("Multiple matches found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig.")
129+
stop(paste("Multiple matches found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Matched schemas: ",
130+
paste(matched_schemas, collapse = ", ")))
127131
} else {
128132
# no match
129-
stop(paste("No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: ",
130-
paste(error_messages, collapse = ", ")))
133+
stop(paste("No match found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Details: >>",
134+
paste(error_messages, collapse = " >> ")))
131135
}
132136

133137
self

samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ test_that("Tests oneOf", {
518518
expect_equal(basque_pig$toJSONString(), original_basque_pig$toJSONString())
519519

520520
# test exception when no matche found
521-
expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: Failed to lookup discriminator value for Pig. Error message: EXPR must be a length 1 vector. Input: \\{\\}, The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
522-
expect_error(pig$validateJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: Failed to lookup discriminator value for Pig. Error message: EXPR must be a length 1 vector. Input: \\{\\}, The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
521+
expect_error(pig$fromJSON('{}'), 'No match found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Details: >> Failed to lookup discriminator value for Pig. Error message: Error! The value of the discriminator property `className`, which should be the class type, is null. JSON input: \\{\\} >> The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\. >> The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
522+
expect_error(pig$validateJSON('{}'), 'No match found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Details: >> Failed to lookup discriminator value for Pig. Error message: Error! The value of the discriminator property `className`, which should be the class type, is null. JSON input: \\{\\} >> The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\. >> The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
523523

524524
# class name test
525525
expect_equal(get(class(basque_pig$actual_instance)[[1]], pos = -1)$classname, "BasquePig")
@@ -587,8 +587,8 @@ test_that("Tests anyOf", {
587587
expect_equal(basque_pig$toJSONString(), original_basque_pig$toJSONString())
588588

589589
# test exception when no matche found
590-
expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
591-
expect_error(pig$validateJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
590+
expect_error(pig$fromJSON('{}'), 'No match found when deserializing the input into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: >> The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\. >> The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
591+
expect_error(pig$validateJSON('{}'), 'No match found when deserializing the input into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: >> The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\. >> The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
592592

593593
})
594594

samples/client/petstore/R-httr2/R/any_of_pig.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ AnyOfPig <- R6::R6Class(
9090
}
9191

9292
# no match
93-
stop(paste("No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: ",
94-
paste(error_messages, collapse = ", ")))
93+
stop(paste("No match found when deserializing the input into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: >>",
94+
paste(error_messages, collapse = " >> ")))
9595
},
9696
#' Serialize AnyOfPig to JSON string.
9797
#'

samples/client/petstore/R-httr2/R/any_of_primitive_type_test.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ AnyOfPrimitiveTypeTest <- R6::R6Class(
102102
self$actual_type <- instance_type
103103
} else if (matched > 1) {
104104
# more than 1 match
105-
stop("Multiple matches found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer.")
105+
stop(paste("Multiple matches found when deserializing the input into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Matched schemas: ",
106+
paste(matched_schemas, collapse = ", ")))
106107
} else {
107108
# no match
108-
stop(paste("No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ",
109-
paste(error_messages, collapse = ", ")))
109+
stop(paste("No match found when deserializing the input into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: >>",
110+
paste(error_messages, collapse = " >> ")))
110111
}
111112

112113
self

samples/client/petstore/R-httr2/R/mammal.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ Mammal <- R6::R6Class(
100100
self$actual_type <- instance_type
101101
} else if (matched > 1) {
102102
# more than 1 match
103-
stop("Multiple matches found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra.")
103+
stop(paste("Multiple matches found when deserializing the input into Mammal with oneOf schemas Whale, Zebra. Matched schemas: ",
104+
paste(matched_schemas, collapse = ", ")))
104105
} else {
105106
# no match
106-
stop(paste("No match found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra. Details: ",
107-
paste(error_messages, collapse = ", ")))
107+
stop(paste("No match found when deserializing the input into Mammal with oneOf schemas Whale, Zebra. Details: >>",
108+
paste(error_messages, collapse = " >> ")))
108109
}
109110

110111
self

samples/client/petstore/R-httr2/R/one_of_primitive_type_test.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ OneOfPrimitiveTypeTest <- R6::R6Class(
102102
self$actual_type <- instance_type
103103
} else if (matched > 1) {
104104
# more than 1 match
105-
stop("Multiple matches found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer.")
105+
stop(paste("Multiple matches found when deserializing the input into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Matched schemas: ",
106+
paste(matched_schemas, collapse = ", ")))
106107
} else {
107108
# no match
108-
stop(paste("No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ",
109-
paste(error_messages, collapse = ", ")))
109+
stop(paste("No match found when deserializing the input into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: >>",
110+
paste(error_messages, collapse = " >> ")))
110111
}
111112

112113
self

samples/client/petstore/R-httr2/R/pig.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ Pig <- R6::R6Class(
100100
self$actual_type <- instance_type
101101
} else if (matched > 1) {
102102
# more than 1 match
103-
stop("Multiple matches found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig.")
103+
stop(paste("Multiple matches found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Matched schemas: ",
104+
paste(matched_schemas, collapse = ", ")))
104105
} else {
105106
# no match
106-
stop(paste("No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: ",
107-
paste(error_messages, collapse = ", ")))
107+
stop(paste("No match found when deserializing the input into Pig with oneOf schemas BasquePig, DanishPig. Details: >>",
108+
paste(error_messages, collapse = " >> ")))
108109
}
109110

110111
self

0 commit comments

Comments
 (0)