Skip to main content

build_request_message

Function build_request_message 

Source
pub fn build_request_message(
    input: &MessageDescriptor,
    mapping: &BodyMapping,
    body: Body<'_>,
    path_params: &HashMap<String, String>,
    raw_query: Option<&str>,
) -> Result<DynamicMessage, String>
Expand description

Build the request message from the body mapping, path params, and query.

Path-bound fields win over the body, and the body wins over query parameters (query only fills fields the body did not set, even to their default). Unknown query, form and path keys are dropped rather than rejected, matching common transcoder behavior; an unknown key in a JSON body is an error, as ProtoJSON has it.

§Errors

A body that is not the ProtoJSON of the mapped message or field, a form body mapped onto a field that is not a message, or a path, query or form value that is not a valid value of its field. The message is meant for the client, as the text of an INVALID_ARGUMENT answer.

§Examples

use std::collections::HashMap;
use prost_reflect::{MessageDescriptor, Value};
use structured_proxy::transcode::request::{build_request_message, Body, BodyMapping};

// message Page { int32 size = 1; string cursor = 2; }
let message = build_request_message(
    &page(),
    &BodyMapping::Root,
    Body::Json(br#"{"size": 0}"#),
    &HashMap::new(),
    Some("size=50&cursor=abc"),
)
.unwrap();
// The body set `size`, to its default: the query cannot replace it.
assert_eq!(message.get_field_by_name("size").unwrap().as_i32(), Some(0));
// It left `cursor` out: the query fills it.
assert_eq!(
    *message.get_field_by_name("cursor").unwrap(),
    Value::String("abc".into())
);