Skip to content

Commit

Permalink
fix grpc: support for uppercase metadata names in static config
Browse files Browse the repository at this point in the history
Tests: Ci
commit_hash:1ccc18a1e838081f42b5b5c35bf37027597e9da0
  • Loading branch information
TTPO100AJIEX committed Oct 29, 2024
1 parent cd60428 commit 7018d33
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions grpc/include/userver/ugrpc/server/metadata_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <boost/range/adaptor/transformed.hpp>

#include <userver/ugrpc/server/rpc.hpp>
#include <userver/utils/assert.hpp>
#include <userver/utils/text.hpp>

USERVER_NAMESPACE_BEGIN

Expand All @@ -21,6 +23,7 @@ namespace ugrpc::server {
/// which are non-owning references to the values of the metadata field.
/// The references must not outlive the call object to avoid undefined behavior.
inline auto GetRepeatedMetadata(ugrpc::server::CallAnyBase& call, std::string_view field_name) {
UASSERT(field_name == utils::text::ToLower(field_name));
const auto& metadata = call.GetContext().client_metadata();
auto [it_begin, it_end] = metadata.equal_range(grpc::string_ref(field_name.data(), field_name.length()));

Expand Down
7 changes: 4 additions & 3 deletions grpc/src/ugrpc/server/middlewares/field_mask/middleware.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "middleware.hpp"

#include <string_view>
#include <utility>

#include <google/protobuf/field_mask.pb.h>
#include <google/protobuf/util/field_mask_util.h>
Expand All @@ -11,6 +10,7 @@
#include <userver/ugrpc/field_mask.hpp>
#include <userver/ugrpc/server/metadata_utils.hpp>
#include <userver/ugrpc/server/middlewares/field_mask/component.hpp>
#include <userver/utils/text.hpp>

USERVER_NAMESPACE_BEGIN

Expand All @@ -26,7 +26,8 @@ FieldMask ConstructFieldMask(ugrpc::server::CallAnyBase& call, std::string_view

} // namespace

Middleware::Middleware(std::string metadata_field_name) : metadata_field_name_(std::move(metadata_field_name)) {}
Middleware::Middleware(std::string metadata_field_name)
: metadata_field_name_(utils::text::ToLower(metadata_field_name)) {}

void Middleware::Handle(ugrpc::server::MiddlewareCallContext& context) const {
try {
Expand All @@ -48,7 +49,7 @@ void Middleware::CallResponseHook(
try {
field_mask.Trim(response);
} catch (const FieldMask::BadPathError& e) {
LOG_WARNING() << "Failed to trim the response " << e.what();
LOG_WARNING() << "Failed to trim the response: " << e.what();
context.GetCall().FinishWithError(::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, e.what()));
}
}
Expand Down
2 changes: 1 addition & 1 deletion grpc/tests/field_mask_middleware_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class UnitTestService final : public sample::ugrpc::UnitTestServiceBase {
class FieldMaskTest : public ugrpc::tests::ServiceFixtureBase {
protected:
FieldMaskTest() {
auto field_mask_middleware = std::make_shared<ugrpc::server::middlewares::field_mask::Middleware>("field-mask");
auto field_mask_middleware = std::make_shared<ugrpc::server::middlewares::field_mask::Middleware>("Field-Mask");
SetServerMiddlewares({field_mask_middleware});
RegisterService(service_);
StartServer();
Expand Down

0 comments on commit 7018d33

Please sign in to comment.