Skip to content

Convert representations if explicitly requested (not via */*) - #1024

Closed
RubenVerborgh wants to merge 4 commits into
mainfrom
fix/markdown
Closed

RubenVerborgh wants to merge 4 commits into
mainfrom
fix/markdown

Conversation

@RubenVerborgh

Copy link
Copy Markdown
Member

Closes #1021
Closes #1023

@RubenVerborgh RubenVerborgh added 🐛 bug Something isn't working semver.patch Does not require a minor or major version bump labels Oct 22, 2021
@RubenVerborgh
RubenVerborgh marked this pull request as draft October 22, 2021 13:03
@RubenVerborgh RubenVerborgh changed the title Fix/markdown Convert representations if explicitly requested (not via */*) Oct 22, 2021
@RubenVerborgh

RubenVerborgh commented Oct 22, 2021 •

Copy link
Copy Markdown
Member Author

@joachimvh There seems to be a downstream bug now:

MacRuben:tmp$ curl http://localhost:3000/markdown.md -i -s -H "Accept: */*" | grep -i content-type
Content-Type: text/markdown
MacRuben:tmp$ curl http://localhost:3000/markdown.md -i -s -H "Accept: text/html" | grep -i content-type
Content-Type: text/html
MacRuben:tmp$ curl http://localhost:3000/markdown.md -i -s -H "Accept: text/html,*/*;q=0.9" | grep -i content-type
Content-Type: application/octet-stream

No idea where the application/octet-stream suddenly comes from.

@RubenVerborgh

Copy link
Copy Markdown
Member Author

No idea where the application/octet-stream suddenly comes from.

It's ContentTypeReplacer.

        {
          "ContentTypeReplacer:_replacements_key": "text/*",
          "ContentTypeReplacer:_replacements_value": "application/octet-stream"
        }

why did we have this?

@RubenVerborgh

Copy link
Copy Markdown
Member Author

why did we have this?

Because it makes sense if a client accepts application/octet-stream that we send text.
But not in the other direction.

@RubenVerborgh
RubenVerborgh removed the request for review from jvherweg October 22, 2021 13:40
@angelo-v

Copy link
Copy Markdown
Contributor

What about this case?

Accept:"text/markdown;q=0.9, text/html;q=0.1"

This should deliver markdown if the server has a markdown representation available.

@RubenVerborgh

Copy link
Copy Markdown
Member Author

This should deliver markdown if the server has a markdown representation available.

Indeed.

@RubenVerborgh RubenVerborgh added semver.minor Requires a minor version bump and removed semver.patch Does not require a minor or major version bump labels Oct 22, 2021
@RubenVerborgh
RubenVerborgh force-pushed the fix/markdown branch 2 times, most recently from 6a4db4c to a7157c5 Compare October 22, 2021 17:20
@RubenVerborgh RubenVerborgh removed their assignment Oct 22, 2021
@RubenVerborgh
RubenVerborgh marked this pull request as ready for review October 22, 2021 17:21
* if set to `false`, always considers */* a match, so conversion is not needed;
* if left `undefined`, only considers */* a match if it has the highest preference score
*/
public constructor(converter: RepresentationConverter = EMPTY_CONVERTER, ignoreWildcards?: boolean) {

@RubenVerborgh RubenVerborgh Oct 22, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ultimately, I ended up not using the ignoreWildcards in the configuration. (At some point, there was a second IfNeededConverter with ignoreWildcards in one of the later steps.) But let's still leave it in as a choice.

@jvherweg jvherweg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you do a GET request on a text/turtle document now with accept header text/html,*/*;q=0.8 (doesn't have to be html specifically) you get as output ... application/trig. This is because the IfNeededConverter gets skipped but the ContentTypeReplacer doesn't. I'm guessing this might not be intended?

Comment thread test/integration/Conversion.test.ts Resolved
Comment thread test/integration/Conversion.test.ts Resolved
Comment thread test/integration/Conversion.test.ts Resolved
Comment thread test/integration/Conversion.test.ts Outdated
headers: { 'content-type': 'text/markdown' },
body: markdownBody,
});
assert.equal(res.status, 201);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
assert.equal(res.status, 201);
assert.strictEqual(res.status, 201);

Comment on lines +57 to +65
if (this.ignoreWildcards !== false && wildcardWeight !== 0) {
// Ignore wildcards if explicitly excluded…
if (this.ignoreWildcards === true) {
typePreferences = { ...typePreferences, '*/*': 0 };
// …or if the request does not have them as the highest preference
} else if (wildcardWeight !== 1 && wildcardWeight !== Math.max(...Object.values(typePreferences))) {
typePreferences = { ...typePreferences, '*/*': 0 };
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a big fan of having all these kinds of behaviours in this class, but probably not the best way to do it. It mostly confirms that moving everything to the chained converter is going to be the cleanest solution (going to be the next PR that I do).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not a big fan of having all these kinds of behaviours in this class

I think it's actually very appropriate here; this is the whole point of IfNeededConverter.

The universal wildcard has a special usage in the Accept header; we shouldn't take it literally as "anything goes" but as "and if needed, I'll take anything else". That's what started #1021, basically.

It mostly confirms that moving everything to the chained converter is going to be the cleanest solution

"Everything" as in "all conversion handlers", sure. But IfNeededConverter allows us to build a nice stack, especially with recent additions. We can basically avoid—at one or multiple places in the conversion chain—to go to a next, more expensive, step when the client is already happy. For instance, Accept: */* does not require any further work, unless for internal types. So we could split all of that up if we wanted to.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not a big fan of having all these kinds of behaviours in this class

I think it's actually very appropriate here; this is the whole point of IfNeededConverter.

The universal wildcard has a special usage in the Accept header; we shouldn't take it literally as "anything goes" but as "and if needed, I'll take anything else". That's what started #1021, basically.

It mostly confirms that moving everything to the chained converter is going to be the cleanest solution

"Everything" as in "all conversion handlers", sure. But IfNeededConverter allows us to build a nice stack, especially with recent additions. We can basically avoid—at one or multiple places in the conversion chain—to go to a next, more expensive, step when the client is already happy. For instance, Accept: */* does not require any further work, unless for internal types. So we could split all of that up if we wanted to.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note the much simpler definition of "already matches" in #834 (comment); this class gives us a more flexible and reusable definition for other classes as well. Even if we will be adopting the chained converter as default, other configurations might not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ChainedConverter already does path optimization to check if it is theoretically possible to find a path with a better weight than the one it has found so far. So it should quickly see that it can immediately return the resource if it matches the strongest accept header (hopefully). But we can potentially continue the discussion once that PR is there.

@RubenVerborgh

Copy link
Copy Markdown
Member Author

If you do a GET request on a text/turtle document now with accept header text/html,*/*;q=0.8 (doesn't have to be html specifically) you get as output ... application/trig. This is because the IfNeededConverter gets skipped but the ContentTypeReplacer doesn't. I'm guessing this might not be intended?

Nope; fixed in 856b2e9
Basically, this can only be fixed by #834

@jvherweg

Copy link
Copy Markdown
Member

The one issue with this PR is that I would say that this is a major semver change now. Due to the ContentTypeReplacer being removed from the default config, several cases that would have worked on the server in the past will not work anymore now.

@RubenVerborgh

Copy link
Copy Markdown
Member Author

Due to the ContentTypeReplacer being removed from the default config, several cases that would have worked on the server in the past will not work anymore now.

I think it's okay. The important cases still work; the ones that do not were broken before.
Conneg is better after this.

@jvherweg

Copy link
Copy Markdown
Member

I think it's okay. The important cases still work; the ones that do not were broken before. Conneg is better after this.

One case I'm thinking of is a resource with content-type application/ld+json. After this change you will get a 501 when sending an application/json accept header, while this would previously work.

Btw, is there a reason this is marked as a minor semver change instead of a patch in that case?

@RubenVerborgh

Copy link
Copy Markdown
Member Author

Btw, is there a reason this is marked as a minor semver change instead of a patch in that case?

New API feature: ignoreWildcards on IfNeededConverter.

@RubenVerborgh

Copy link
Copy Markdown
Member Author

One case I'm thinking of is a resource with content-type application/ld+json. After this change you will get a 501 when sending an application/json accept header, while this would previously work.

Can re-add that one. But that would indeed not work for Turtle then (and probably never has).

@jvherweg

jvherweg commented Nov 5, 2021

Copy link
Copy Markdown
Member

@RubenVerborgh Shall we close this one then?

@RubenVerborgh

Copy link
Copy Markdown
Member Author

Not sure about killing IfNeededConverter, but yes!

@jvherweg
jvherweg deleted the fix/markdown branch February 1, 2022 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working semver.minor Requires a minor version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Representation conversion is performed even with Accept */* markdown resources cannot be identified because CSS cloaks them as HTML

3 participants