question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Header deserialization should be case insensitive

See original GitHub issue
  • Package Name: core-http
  • Package Version: 2.2.5 and 2.2.6
  • Operating system: all
  • nodejs
    • version: all
  • browser
    • name/version:
  • typescript
    • version: all
  • Is the bug related to documentation in

Describe the bug

We are using a custom HTTP client to preserve header casings as they come from the server. When we do this we do something like:

// the object our sendRequest method returns
const httpOperationResponse: HttpOperationResponse = {
    request: httpRequest,
    status: response.statusCode,
    // response.rawHeaders is a collection of raw HTTP response headers exactly as they were received.
    // Header keys and values come in a single list and not as a list of tuples.
    // Header keys are immediately followed by their respective values.
    // Header names are not lowercased, and duplicates are not merged.
    headers: this.#parseNetRawHeaders(response.rawHeaders),
    readableStreamBody: undefined,
    bodyAsText: undefined
};

and

#parseNetRawHeaders(rawHeaders): HttpHeadersLike {
    const httpHeaders = new CoreHttp.HttpHeaders();
    for (let i = 0; i < rawHeaders.length; i += 2) {
        httpHeaders.set(rawHeaders[i], rawHeaders[i + 1]);
    }
    return httpHeaders;
}

We recently started noticing that this introduces problems during deserialization of headers in core-http’s deserializeCompositeType. Specifically in this code block:

else {
        const property = responseBody[propertyName];
        instance[key] = serializer.deserialize(propertyMapper, property, propertyObjectName, options);
}

The propertyName is an all-lowercase version of the header, while the responseBody contains the headers with original-cased names. image

This should be handled better, as HTTP headers should be treated case insensitively by code. It should not matter what case the server used for a header. Since the code assumes that responseBody contains headers with lowercased names, then deserialization of headers can fail (such as the last-modified example in my screenshot).

To Reproduce

  1. Do as described above

Expected behavior Deserialization should work.

Screenshots See above.

Additional context Possible solutions:

  1. In the short-term we will consider being more stringent about what headers we respect casing for.
  2. In the long-term we would expect this to be fixed in core-http. Perhaps the best spot to do this would be in deserializeResponseBody when the headers are deserialized. Instead of passing in parsedResponse.headers.rawHeaders() to operationSpec.serializer.deserialize, y’all could use parsedResponse.headers.toJson({ preserveCase: false })? If operationSpec.serializer.deserialize is going to assume header keys have been lowercased, using rawHeaders/headers which are potentially uppercase probably isn’t a good idea.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MRayermannMSFTcommented, Aug 13, 2022

@xirzec I tested it locally and it seems to work well/as expected.

0reactions
xirzeccommented, Aug 16, 2022

@MRayermannMSFT looks like this actually made the nightly (which is really a daily) build yesterday. Give @dev or 2.2.7-alpha.20220815.2 a try!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize response headers as case-insensitive #195 - GitHub
HTTP header names are specified as case insensitive, so let's do the following to solve this: Convert header names on the response to...
Read more >
Jackson - Case insensitive deserialization - m. tarık yurt
How to deserialize case insensitive JSON data. ... This JSON string is a valid one and it can be easily deserialized by following...
Read more >
Deserialize enum ignoring case in Spring Boot controller
I have used both: spring boot property and enabled on custom object mapper. Custom ObjectMapper config is marked as \@Bean \@Primary so it ......
Read more >
How to enable case-insensitive property name matching with ...
By default, deserialization looks for case-sensitive property name matches between JSON and the target object properties.
Read more >
JSON is Case Sensitive. You Don't Have to Be. - Couchbase
JSON is case sensitive to both field names and data. So is N1QL. In this article, we'll discuss dealing with data case sensitivity....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found