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

Complain about values to be string instead of integers #211

Closed
germanolleunlp opened this issue Jan 8, 2020 · 6 comments
Closed

Complain about values to be string instead of integers #211

germanolleunlp opened this issue Jan 8, 2020 · 6 comments

Comments

@germanolleunlp
Copy link

Seems that some services expect to have these values as strings instead of integers.

 "error": {
  "message": "500 - \"Rack::Lint::LintError: a header value must be a String, but the value of 'Current-Page' is a Integer\\n\\t/app/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/lint.rb:20:in `assert'\\n\\t/app/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/lint.rb:645:in `block in check_headers'\\n\\t/app/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/utils.rb:454:in `block in each'\\n\\t/app/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/utils.rb:453:in `each'\\n\\t/app/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/r",
  "method": "GET",
  "uri": "https://www-bay-umt-lms-stg.2u.com/ap/api/lti/v1p3/context/079ff368-9bc5-4a57-874b-21838cb509f2/lineitems?resource_link_id=079ff368-9bc5-4a57-874b-21838cb509f2:c99a1343-d558-44b5-b781-504290f5d599&tag=file"
 },
 "data": {
  "id": 750,
  "created_at": "2020-01-07T22:22:55.856Z",
  "last_modified_at": "2020-01-07T22:22:56.817Z",
  "lis_result_sourcedid": "MDc5ZmYzNjgtOWJjNS00YTU3LTg3NGItMjE4MzhjYjUwOWYyOjo6MDc5ZmYzNjgtOWJjNS00YTU3LTg3NGItMjE4MzhjYjUwOWYyOmM5OWExMzQzLWQ1NTgtNDRiNS1iNzgxLTUwNDI5MGY1ZDU5OTo6OjAwMDAwMDAwLTAwMDAtNDAwMC1hMDAwLTAwMDAwMDAwMDAyMg==",
  "paper_id": "1239863085",
  "ext_outcomes_tool_placement_url": "https://api.turnitin.com/api/lti/1p0/outcome_tool_data/1239863085?lang=en_us",
  "s3_path": "s3://2u-aio-test/4fedf8d5-2f48-442d-9a84-cd9b08149479/4fedf8d5-2f48-442d-9a84-cd9b08149479",
  "score": null,
  "resource_link_id": "079ff368-9bc5-4a57-874b-21838cb509f2:c99a1343-d558-44b5-b781-504290f5d599",
  "context_id": "079ff368-9bc5-4a57-874b-21838cb509f2",
  "user_id": "00000000-0000-4000-a000-000000000022",
  "program": "BAY-MSW",
  "lmsdomain": "https://www-bay-umt-lms-stg.2u.com",
  "grading_progress": null,
  "activity_progress": null,
  "mime_type": "application/pdf",
  "uuid": "4fedf8d5-2f48-442d-9a84-cd9b08149479",
  "size": 433994,
  "name": "examplepdf.pdf",
  "context_type": "course_section",
  "original_resource_link_id": "c99a1343-d558-44b5-b781-504290f5d599",
  "grade": null,
  "max": "2020-01-07T22:22:55.856Z"
 }
}

One workaround is override this method but could be useful to return these values directly as strings or provide an easy way to customize this.

# Convert all values of Current-Page, Page-Items, Total-Pages and Total-Count into string
def pagy_headers_hash(pagy)
  Hash[super(pagy).to_a.map { |key, value| [key, key.match?(/-/) ? value.to_s : value] }]
end
@ddnexus
Copy link
Owner

ddnexus commented Jan 9, 2020

True! Headers values must be ASCI strings: I thought that the conversion would be done automatically by Rack, but it looks like that's not the case.

I will change the integers to strings directly in the pagy_headers_hash.
Thanks

ddnexus added a commit that referenced this issue Jan 9, 2020
@ddnexus ddnexus closed this as completed Jan 9, 2020
@germanolleunlp
Copy link
Author

True! Headers values must be ASCI strings: I thought that the conversion would be done automatically by Rack, but it looks like that's not the case.

I will change the integers to strings directly in the pagy_headers_hash.
Thanks

Thanks, any ETO this be pushed in rubygems?

@ddnexus
Copy link
Owner

ddnexus commented Jan 13, 2020

Sorry for the late answer. I was waiting to add a couple of commits, but it looks like I have no time. Tomorrow I will be able to push 6a93b97

@ddnexus
Copy link
Owner

ddnexus commented Jan 14, 2020

Pushed to Rubygems

@guillaumebriday
Copy link

Hey !

Thank you @ddnexus and @germanolleunlp

I think it's a good PR but this should be release as a major version because I think it's could be a breaking change for lots of applications.

I'm pretty sure that lots of people calculate their next page by doing something like: Current-Page + 1

With integer, it's an addition like: 1 + 1 => 2

But with string it's a concatenation like so: '1' + 1 => 11

Using parseInt do the job but developers need to be aware of that change more explicitly I think.

Thanks

@ddnexus ddnexus reopened this Jan 22, 2020
@ddnexus
Copy link
Owner

ddnexus commented Jan 22, 2020

While this is a considerate concern, it doesn't look like the change can break much if anything at all.

The headers returned to the client were strings before the change and they are still strings after the change, so whatever code was working on the client side will still work.

On the server side the pagy_headers_hash is used internally to return the hash of strings that rack will use to produce the headers. That is not intended to be used in any other ways such as calculations of pages or other stuff: it does the calculations for you.

Of course you can override the method if you want to change its logic... and in that case you are in total control of the calculation and returned values.

The change could in theory break overridden pagy_headers_hash methods that produce some sort of custom headers involving page numbers and calling the original method instead of rewriting it.

Consider that if you need custom headers involving page numbers, you are probably working with an API, and in that case you are probably using the metadata extra, not the standard headers extra.

That complex interaction of coincidences looks like a very remote possibility to me.

But even if that rare case happens, spotting the problem (by running the app just once) and fixing it is so trivial that I could be considered as part of the normal routine adjustment when you upgrade any gem, especially if you overrode something of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants