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.

[BUG][ruby-client] Body variable not properly serialized when named `body`

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When the body variable is named body, this line of code makes it bypass the call to @api_client.object_to_http_body, which prevents the transformation of the object into a hash, thus making to_json return invalid JSON.

At Datadog, we make extensive use of the x-codegen-request-body-name extension and set it to body for consistency and ease of use across our different endpoints, which causes this issue.

openapi-generator version

5.0.0-beta

OpenAPI declaration file content or url

The following example will generate

paths:
  /send_body:
    post:
      x-codegen-request-body-name: body
      requestBody:
        content:
          "application/json":
            schema:
              $ref: "#/components/schemas/BodyParam"
      responses:
        "200":
          description: OK
components:
  schemas:
    BodyParam:
      type: object
      properties:
        prop:
          type: string 
Steps to reproduce

Generating the ruby client for the above endpoint will create this code:

      # http body (model)
      post_body = opts[:body] || @api_client.object_to_http_body(opts[:'body']) 

Now when using the endpoint, you would do something like the following

body_param = BodyParam.new
body_param.prop = "foo"
api.send_body({body: body_param})

Because of the generated line of code above, the object will be passed as is to the call_api method, and the JSON serialization will produce invalid JSON, as the object did not go through the object_to_http_body method:

"{:prop=>\"foo\"}"
Suggest a fix

Looking for suggestions on how to fix this. I’d be happy to implement it.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
wing328commented, Sep 14, 2020

I’ll talk to that PR author about the additional logic to pass various options (parameter values) to the method.

(other clients do not have similar logic as the method should work according to the contract/spec)

0reactions
zippolytecommented, Sep 15, 2020

Thanks, this solves my use case as well, so I’d be okay to merge. I just hope no one has a body variable named debug_body 😅. For the time being though I’m 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

RestSharp AddJsonBody is Not Serializing Body Correctly
The API expects an array of AdoRequestBody objects and you are serializing a single object. As stated in comments, create an array with...
Read more >
objectMapper.writeValueAsString does not properly serialize ...
Describe the bug When calling the objectMapper's writeValueAsString and passing a Kotlin data class to the function, any of the variables ...
Read more >
Understanding TypeScript object serialization - LogRocket Blog
HTTP uses serialization when transmitting data through a web browser. For example, a RESTFul service has an HTTP request with a message body...
Read more >
Android Data Serialization Tutorial with the Kotlin Serialization ...
Learn how to use the Kotlin Serialization library in your Android app ... Then, open the starter project, Bored No More!, in Android...
Read more >
Serializing Request and Response Bodies - Aqueduct
The default constructor takes a status code, header map and body object. There are many named constructors for common response types:.
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