From 2303ce1803eb4c029c416d703fe3f9796692e85d Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Sat, 7 Dec 2024 14:45:59 -0800 Subject: [PATCH] chore: regenerate samples with enum validation --- samples/learn/schema/StarWarsSharedTypes.cpp | 9 ++++++++- samples/proxy/schema/ProxySharedTypes.cpp | 9 ++++++++- .../today/nointrospection/TodaySharedTypes.cpp | 9 ++++++++- samples/today/schema/TodaySharedTypes.cpp | 9 ++++++++- .../schema/ValidationSharedTypes.cpp | 18 ++++++++++++++++-- src/SchemaGenerator.cpp | 2 ++ src/introspection/IntrospectionSharedTypes.cpp | 18 ++++++++++++++++-- 7 files changed, 66 insertions(+), 8 deletions(-) diff --git a/samples/learn/schema/StarWarsSharedTypes.cpp b/samples/learn/schema/StarWarsSharedTypes.cpp index b1034fe2..48fcf3a9 100644 --- a/samples/learn/schema/StarWarsSharedTypes.cpp +++ b/samples/learn/schema/StarWarsSharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::AwaitableSca return ModifiedResult::resolve(std::move(result), std::move(params), [](learn::Episode value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesEpisode[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesEpisode.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for Episode)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesEpisode[idx] } } } }; }); } diff --git a/samples/proxy/schema/ProxySharedTypes.cpp b/samples/proxy/schema/ProxySharedTypes.cpp index 8fba4aa3..d58c8f99 100644 --- a/samples/proxy/schema/ProxySharedTypes.cpp +++ b/samples/proxy/schema/ProxySharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::Awaita return ModifiedResult::resolve(std::move(result), std::move(params), [](proxy::OperationType value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesOperationType[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesOperationType.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for OperationType)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesOperationType[idx] } } } }; }); } diff --git a/samples/today/nointrospection/TodaySharedTypes.cpp b/samples/today/nointrospection/TodaySharedTypes.cpp index 2d9d50ca..727a3253 100644 --- a/samples/today/nointrospection/TodaySharedTypes.cpp +++ b/samples/today/nointrospection/TodaySharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::AwaitableS return ModifiedResult::resolve(std::move(result), std::move(params), [](today::TaskState value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTaskState[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesTaskState.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for TaskState)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTaskState[idx] } } } }; }); } diff --git a/samples/today/schema/TodaySharedTypes.cpp b/samples/today/schema/TodaySharedTypes.cpp index 2d9d50ca..727a3253 100644 --- a/samples/today/schema/TodaySharedTypes.cpp +++ b/samples/today/schema/TodaySharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::AwaitableS return ModifiedResult::resolve(std::move(result), std::move(params), [](today::TaskState value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTaskState[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesTaskState.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for TaskState)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTaskState[idx] } } } }; }); } diff --git a/samples/validation/schema/ValidationSharedTypes.cpp b/samples/validation/schema/ValidationSharedTypes.cpp index 4db548fa..84e32700 100644 --- a/samples/validation/schema/ValidationSharedTypes.cpp +++ b/samples/validation/schema/ValidationSharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::Awai return ModifiedResult::resolve(std::move(result), std::move(params), [](validation::DogCommand value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesDogCommand[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesDogCommand.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for DogCommand)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesDogCommand[idx] } } } }; }); } @@ -102,7 +109,14 @@ service::AwaitableResolver Result::convert(service::Awai return ModifiedResult::resolve(std::move(result), std::move(params), [](validation::CatCommand value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesCatCommand[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesCatCommand.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for CatCommand)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesCatCommand[idx] } } } }; }); } diff --git a/src/SchemaGenerator.cpp b/src/SchemaGenerator.cpp index 5c3fb8cd..1a0351d4 100644 --- a/src/SchemaGenerator.cpp +++ b/src/SchemaGenerator.cpp @@ -1692,12 +1692,14 @@ service::AwaitableResolver Result<)cpp" << R"cpp(::)cpp" << enumType.cppType << R"cpp( value, const ResolverParams&) { const size_t 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" } }; } + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_names)cpp" << enumType.cppType << R"cpp([idx] } } } }; }); diff --git a/src/introspection/IntrospectionSharedTypes.cpp b/src/introspection/IntrospectionSharedTypes.cpp index ba9a3e7e..7d47c719 100644 --- a/src/introspection/IntrospectionSharedTypes.cpp +++ b/src/introspection/IntrospectionSharedTypes.cpp @@ -50,7 +50,14 @@ service::AwaitableResolver Result::convert(service::Awa return ModifiedResult::resolve(std::move(result), std::move(params), [](introspection::TypeKind value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTypeKind[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesTypeKind.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for __TypeKind)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesTypeKind[idx] } } } }; }); } @@ -102,7 +109,14 @@ service::AwaitableResolver Result::convert(ser return ModifiedResult::resolve(std::move(result), std::move(params), [](introspection::DirectiveLocation value, const ResolverParams&) { - return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesDirectiveLocation[static_cast(value)] } } } }; + const size_t idx = static_cast(value); + + if (idx >= s_namesDirectiveLocation.size()) + { + throw service::schema_exception { { R"ex(Enum value out of range for __DirectiveLocation)ex" } }; + } + + return ResolverResult { { response::ValueToken::EnumValue { std::string { s_namesDirectiveLocation[idx] } } } }; }); }