Skip to content

Commit

Permalink
Merge pull request #340 from wravery/issues-334
Browse files Browse the repository at this point in the history
fix: #334
  • Loading branch information
wravery authored Dec 8, 2024
2 parents 681b0e1 + 81abdc6 commit a227b09
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
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

0 comments on commit a227b09

Please sign in to comment.