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.

Response with text media type is parsed to a JSON object instead of a string

See original GitHub issue

The schema registry service returns schemas as text/plain. Avro schemas happen to be JSON, but future serialization formats could represent their schema in text that is not JSON.

Prior version of schema registry swagger indicated to send the schema as a string in JSON. So the body was quoted and escaped. It was very confusing. Now the body is just text/plain, UTF-8. However, upon regenerating the code from the new swagger, I started getting back the body as parsed JSON object, even though the generated code typed it as string.

I’m not sure if this is an issue with the core ServiceClient or with the code that autorest has generated. I do see mediaType: "text" in the generated code FWIW.

EDIT: The issue appears to be that the service doesn’t actually response with Content-Type: text/plain. Self assigning, tagging schema registry and following up with service owners.

Repro steps

  1. Check out https://github.com/Azure/azure-sdk-for-js/pull/11650
  2. Apply the following patch
diff --git a/sdk/schemaregistry/schema-registry/src/conversions.ts b/sdk/schemaregistry/schema-registry/src/conversions.ts
index 35c60a467..09121d5b8 100644
--- a/sdk/schemaregistry/schema-registry/src/conversions.ts
+++ b/sdk/schemaregistry/schema-registry/src/conversions.ts
@@ -30,10 +30,7 @@ type GeneratedResponse = GeneratedSchemaResponse | GeneratedSchemaIdResponse;
  * @internal
  */
 export function convertSchemaResponse(response: GeneratedSchemaResponse): Schema {
-  // https://github.com/Azure/azure-sdk-for-js/issues/11649
-  // Although response.body is typed as string, it is a parsed JSON object,
-  // so we use _response.bodyAsText instead as a workaround.
-  return convertResponse(response, { content: response._response.bodyAsText });
+  return convertResponse(response, { content: response.body });
 }
  1. rush update && rush build
  2. cd sdk/schemaregistry/schema-registry
  3. npm run test

Expected result

Tests pass

Actual result

Tests fail:

  1 failing

  1) SchemaRegistryClient
       gets schema by ID:
     AssertionError: expected { Object (type, name, ...) } to equal '{"type":"record","name":"User","namespace":"com.azure.schemaregistry.samples","fields":[{"name":"name","type":"string"},{"name":"favoriteNumber","type":"int"}]}'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
deyaaeldeencommented, Aug 18, 2021

Quick update: @hmlam and @nickghardwick will update the service to return responses with content-type of text/plain;charset=UTF-8.

1reaction
jeremymengcommented, Oct 5, 2020

@nguerrera Looks like this is a recorded test, and the recorded response has content-type of application/json? So core-http will de-serialize it from JSON to object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON.parse returns string instead of object - Stack Overflow
This seems to be fairly consistent with over-stringified strings. For example I loaded a text file using FileReader.readAsText that came with \n and...
Read more >
Sometime response.data is string instead of object #1723
I got this problem, too. When i check response.data of problem case, some invalid JSON String like " data added to response, that...
Read more >
JSON.parse() - JavaScript - MDN Web Docs
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Read more >
JSON Stringify Example – How to Parse a JSON Object with JS
json ()) // the . json() method parses the JSON response into a JS object literal . then(data => console. log(data));
Read more >
How To Use JSON.parse() and JSON.stringify() - DigitalOcean
JSON.parse() takes a JSON string and transforms it into a JavaScript object. let userStr = '{"name": ...
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