Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #334 #340

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion samples/learn/schema/StarWarsSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ service::AwaitableResolver Result<learn::Episode>::convert(service::AwaitableSca
return ModifiedResult<learn::Episode>::resolve(std::move(result), std::move(params),
[](learn::Episode value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesEpisode[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesEpisode[idx] });

return resolvedResult;
});
Expand Down
9 changes: 8 additions & 1 deletion samples/today/nointrospection/TodaySchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ service::AwaitableResolver Result<today::TaskState>::convert(service::AwaitableS
return ModifiedResult<today::TaskState>::resolve(std::move(result), std::move(params),
[](today::TaskState value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesTaskState[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesTaskState[idx] });

return resolvedResult;
});
Expand Down
9 changes: 8 additions & 1 deletion samples/today/schema/TodaySchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ service::AwaitableResolver Result<today::TaskState>::convert(service::AwaitableS
return ModifiedResult<today::TaskState>::resolve(std::move(result), std::move(params),
[](today::TaskState value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesTaskState[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesTaskState[idx] });

return resolvedResult;
});
Expand Down
18 changes: 16 additions & 2 deletions samples/validation/schema/ValidationSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ service::AwaitableResolver Result<validation::DogCommand>::convert(service::Awai
return ModifiedResult<validation::DogCommand>::resolve(std::move(result), std::move(params),
[](validation::DogCommand value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesDogCommand[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesDogCommand[idx] });

return resolvedResult;
});
Expand Down Expand Up @@ -110,9 +117,16 @@ service::AwaitableResolver Result<validation::CatCommand>::convert(service::Awai
return ModifiedResult<validation::CatCommand>::resolve(std::move(result), std::move(params),
[](validation::CatCommand value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesCatCommand[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesCatCommand[idx] });

return resolvedResult;
});
Expand Down
11 changes: 10 additions & 1 deletion src/SchemaGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(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>(std::string { s_names)cpp"
<< enumType.cppType << R"cpp([static_cast<size_t>(value)] });
<< enumType.cppType << R"cpp([idx] });

return resolvedResult;
});
Expand Down
18 changes: 16 additions & 2 deletions src/introspection/IntrospectionSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ service::AwaitableResolver Result<introspection::TypeKind>::convert(service::Awa
return ModifiedResult<introspection::TypeKind>::resolve(std::move(result), std::move(params),
[](introspection::TypeKind value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesTypeKind[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesTypeKind[idx] });

return resolvedResult;
});
Expand Down Expand Up @@ -104,9 +111,16 @@ service::AwaitableResolver Result<introspection::DirectiveLocation>::convert(ser
return ModifiedResult<introspection::DirectiveLocation>::resolve(std::move(result), std::move(params),
[](introspection::DirectiveLocation value, const ResolverParams&)
{
const auto idx = static_cast<size_t>(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>(std::string { s_namesDirectiveLocation[static_cast<size_t>(value)] });
resolvedResult.set<std::string>(std::string { s_namesDirectiveLocation[idx] });

return resolvedResult;
});
Expand Down
Loading