Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse JSON when lower-case content-type is given #440

Merged
merged 4 commits into from
Feb 6, 2025

Conversation

mopp
Copy link
Contributor

@mopp mopp commented Feb 5, 2025

Thank you for your nice gem to make our application robust.
I would like to fix a small problem.

Problem to be solved

committee doesn't parse the response when content-type: application/json header is given.
Then, the validation failed due to comparing an object and String.

{
    "id":"invalid_response",
    "message":"#/paths/~1characters/get/responses/200/content/application~1json/schema expected object, but received String: \"{\\\"Otonokizaka\\\":[\\\"Honoka.Kousaka\\\"]}\""
}

How to solve it

Parse also the response when content-type: application/json.

Alternative

Convert the given headers into Rack::Headers always at

status, headers, response = @app.call(request.env)

But the Rack::Headers was introduced in rack 3.0.0.beta1
So we need to change the committee requirement in this way.

s.add_dependency "rack", ">= 1.5", "< 3.2" 

References

@geemus
Copy link
Member

geemus commented Feb 5, 2025

Hey, thanks for the detailed report and references. I think it might make sense to think about using an approach very similar to what we did in openapi_parser. At least presently we only really care about the content-type header, so we probably don't need the full Rack headers stuff, but it would still be good to allow any variation of capital letters for content type (so lower, title, upper or whatever mix really).

So we could use a detect + casecmp to figure out the variation in use:

    content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }

And then lookup the value underlying that:

    headers.fetch(content_type_key, nil)&.start_with?('application/json')

I think that feels like a relatively minimal change and would resolve the issue. What do you think?

Beyond that it looks like there might be some lint issues for us to cleanup, but we can deal with that once we settle on a fix. Thanks!

@mopp
Copy link
Contributor Author

mopp commented Feb 6, 2025

@geemus Thank you for reviewing ✨
Your way looks good and I followed them.

I pushed the changes separately. But I can squash them into a commit if you prefer.

Copy link
Member

@geemus geemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@geemus geemus merged commit ddec9ed into interagent:master Feb 6, 2025
8 checks passed
@geemus
Copy link
Member

geemus commented Feb 6, 2025

released in v5.5.1

@mopp mopp deleted the case-insensitive-content-type branch February 6, 2025 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants