-
-
Notifications
You must be signed in to change notification settings - Fork 15
JSON Hyper-Schema usage with Express: #1
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
Conversation
* Adding capability to parse a content-type header like so: Content-Type: application/my-media-type+json; profile=http://example.com/my-hyper-schema# * See: http://json-schema.org/latest/json-schema-core.html, section 8.1. Correlation by means of the "Content-Type" header
* Only need on slash to match slashes
Well, you can't just change a regular express that matches an RFC. Can you either show me how the regular expression does not correctly match RFC 2616, or give me what RFC your change matches? |
* Updating version for npm
Anyway, The valid content-type would be |
Though parsing the parameter |
FYI, the description for the |
I'm going to re-open to think on this for a while because RFC 6838 states there is no actual defined format for parameter values, but the transport (HTTP in this case) can impose their own restriction (like, for example, that For example, a solution I may do here is parse according to RFC 6838 (which would parse your example correctly), but provide an option to stringify as RFC 6838 or RFC 7230. I'm still thinking about it. |
Hey Doug, Thanks for reviewing this PR. I see what you are saying that there is no defined syntax for parameter values, and yet still, some transfer protocols (http) may enforce their own syntax. I like your idea to make it an option. I am trying to think of ways in which this change would affect end users of your package. I know express uses media-typer, and express does use an HTTP protocol. express:lib/util.js exports.setCharset = function(type, charset){
if (!type || !charset) return type;
// parse type
var parsed = typer.parse(type);
// set charset
parsed.parameters.charset = charset;
// format type
return typer.format(parsed);
}; Here format also imposes some syntax, by quoting certain parameters: // append parameters
if (parameters && typeof parameters === 'object') {
var param
var params = Object.keys(parameters).sort()
for (var i = 0; i < params.length; i++) {
param = params[i]
if (!tokenRegExp.test(param)) {
throw new TypeError('invalid parameter name')
}
string += '; ' + param + '=' + qstring(parameters[param])
}
} Perhaps this is enough; although it would be nice not to quote a profile parameter value. (I also considered making this change PR: removing the qstring call, and just assigning the raw value)
|
Well, I know how express is using it, because I wrote it in there :)
that's just not something I'm going to allow if the media type is going to end up in the |
Doug, Thanks for clarifying. Now I see how qstring is necessary for following the HTTP protocol used in express. Does it seem to you that JSON Hyper-Schema is disobeying the HTTP spec? The reason I ask is because, I am running into another issue, where a client library that follows the JSON-Schema spec is doing the following: This repo appends the quoted profile value to the current url, and asks for the schema there:
and makes a schema request to the profile value when responded without quotes.
I can follow up with this repo owner, and I'd also like you feedback, because the JSON Schema seems to be wrong here. |
@@ -1,7 +1,7 @@ | |||
{ | |||
"name": "media-typer", | |||
"description": "Simple RFC 6838 media type parser and formatter", | |||
"version": "0.2.0", | |||
"version": "0.2.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please never, ever, do this in PR's.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fishrock123 Thanks, I'll remove the modification to the package.json commit.
Only if that string example is what is literally set as the
Sorry, I'm not following what you are seeing from that quote and below in your comment. We are talking about the This is one of the reasons we are working to get things like this library in the |
Doug, As far is this PR, Do you still think it's worth considering changing the regex? Thanks for pointing out that line. I'll follow up with the other repo owner. |
The regex changed specifically is meant to exactly match that from the RFC in the comment above it, so the regex there will never change. But this PR still brings up a couple issues that I'll want to address:
|
Thanks for thinking about this. What would a valid profile parameter on a Content-type look like for HTTP protocol? |
Here is the example I commented above:
|
Thanks Doug. |
* Reverting version update
* Don't need to add quotations around Media Type parameter
Thanks for all your help Doug, I know how to support what I want to do, and it involves removing the qstring and changing the regex. After adding quotes to profile (before handing it off to express' setHeader method), media-typer still strips out the url value. That won't work for JSON Hyper Schema. If you would like I can leave this PR open, or I can close it for now. I am going to use my own fork of this repo in my project until I better understand how I can make this functionality work for everyone. |
Yes, please just leave it open for now, because it brings up some issues in here, so it's a good reminder.
I have no idea what you mean. Can you show me code that does that? I'm using this: var typer = require('media-typer')
var type = 'application/json; charset=utf-8; profile="http://localhost:8080/schema/homes/home/schema.json#"'
var parsed = typer.parse(type)
parsed.parameters.charset = 'iso-8859-1'
console.log(typer.format(parser))
// logs application/json; charset=iso-8859-1; profile="http://localhost:8080/schema/homes/home/schema.json#" and as you can see, it's clearly not stripping out the quoted profile. |
OK, my mistake, I can verify it works like you show, when using your test script. I guess I just needed to add the quotations...and that solves my issue. Thanks again. |
Content-Type: application/my-media-type+json;
profile=http://example.com/my-hyper-schema#
I want to use express as a hypermedia server, for a JSON Hyper-Schema driven API. The JSON-Schema core spec defines in section 8.1 a profile parameter, on Content-type headers; That parameter accepts a uri, including the http, "://", fqdn, and path, as well as a potential #hash-tag. This PR adds the ability for media-typer to handle this type of profile parameter.
Without this change, media-typer would parse the following profile parameter:
as:
With this change:
Also, this change still successfully parses:
as: