diff --git a/samples/learn/schema/StarWarsSchema.cpp b/samples/learn/schema/StarWarsSchema.cpp index c14d3696..c3ce0a24 100644 --- a/samples/learn/schema/StarWarsSchema.cpp +++ b/samples/learn/schema/StarWarsSchema.cpp @@ -53,9 +53,16 @@ service::AwaitableResolver Result::convert(service::AwaitableSca return ModifiedResult::resolve(std::move(result), std::move(params), [](learn::Episode value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesEpisode.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for Episode)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesEpisode[static_cast(value)] }); + resolvedResult.set(std::string { s_namesEpisode[idx] }); return resolvedResult; }); diff --git a/samples/today/nointrospection/TodaySchema.cpp b/samples/today/nointrospection/TodaySchema.cpp index aee07aac..55d3f6ae 100644 --- a/samples/today/nointrospection/TodaySchema.cpp +++ b/samples/today/nointrospection/TodaySchema.cpp @@ -54,9 +54,16 @@ service::AwaitableResolver Result::convert(service::AwaitableS return ModifiedResult::resolve(std::move(result), std::move(params), [](today::TaskState value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesTaskState.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for TaskState)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesTaskState[static_cast(value)] }); + resolvedResult.set(std::string { s_namesTaskState[idx] }); return resolvedResult; }); diff --git a/samples/today/schema/TodaySchema.cpp b/samples/today/schema/TodaySchema.cpp index d11e9e23..ddde3725 100644 --- a/samples/today/schema/TodaySchema.cpp +++ b/samples/today/schema/TodaySchema.cpp @@ -54,9 +54,16 @@ service::AwaitableResolver Result::convert(service::AwaitableS return ModifiedResult::resolve(std::move(result), std::move(params), [](today::TaskState value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesTaskState.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for TaskState)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesTaskState[static_cast(value)] }); + resolvedResult.set(std::string { s_namesTaskState[idx] }); return resolvedResult; }); diff --git a/samples/validation/schema/ValidationSchema.cpp b/samples/validation/schema/ValidationSchema.cpp index 9a49b7e7..e5d18879 100644 --- a/samples/validation/schema/ValidationSchema.cpp +++ b/samples/validation/schema/ValidationSchema.cpp @@ -54,9 +54,16 @@ service::AwaitableResolver Result::convert(service::Awai return ModifiedResult::resolve(std::move(result), std::move(params), [](validation::DogCommand value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesDogCommand.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for DogCommand)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesDogCommand[static_cast(value)] }); + resolvedResult.set(std::string { s_namesDogCommand[idx] }); return resolvedResult; }); @@ -110,9 +117,16 @@ service::AwaitableResolver Result::convert(service::Awai return ModifiedResult::resolve(std::move(result), std::move(params), [](validation::CatCommand value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesCatCommand.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for CatCommand)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesCatCommand[static_cast(value)] }); + resolvedResult.set(std::string { s_namesCatCommand[idx] }); return resolvedResult; }); diff --git a/src/SchemaGenerator.cpp b/src/SchemaGenerator.cpp index 59c41549..b9aeb3c7 100644 --- a/src/SchemaGenerator.cpp +++ b/src/SchemaGenerator.cpp @@ -1336,10 +1336,19 @@ service::AwaitableResolver Result<)cpp" []()cpp" << _loader.getSchemaNamespace() << R"cpp(::)cpp" << enumType.cppType << R"cpp( value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_names)cpp" + << enumType.cppType << R"cpp(.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for )cpp" + << enumType.type << R"cpp()ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); resolvedResult.set(std::string { s_names)cpp" - << enumType.cppType << R"cpp([static_cast(value)] }); + << enumType.cppType << R"cpp([idx] }); return resolvedResult; }); diff --git a/src/introspection/IntrospectionSchema.cpp b/src/introspection/IntrospectionSchema.cpp index 81b28bf4..b87861cd 100644 --- a/src/introspection/IntrospectionSchema.cpp +++ b/src/introspection/IntrospectionSchema.cpp @@ -48,9 +48,16 @@ service::AwaitableResolver Result::convert(service::Awa return ModifiedResult::resolve(std::move(result), std::move(params), [](introspection::TypeKind value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesTypeKind.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for __TypeKind)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesTypeKind[static_cast(value)] }); + resolvedResult.set(std::string { s_namesTypeKind[idx] }); return resolvedResult; }); @@ -104,9 +111,16 @@ service::AwaitableResolver Result::convert(ser return ModifiedResult::resolve(std::move(result), std::move(params), [](introspection::DirectiveLocation value, const ResolverParams&) { + const auto idx = static_cast(value); + + if (idx >= s_namesDirectiveLocation.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for __DirectiveLocation)ex" } }; + } + response::Value resolvedResult(response::Type::EnumValue); - resolvedResult.set(std::string { s_namesDirectiveLocation[static_cast(value)] }); + resolvedResult.set(std::string { s_namesDirectiveLocation[idx] }); return resolvedResult; });