8000 [WebLink] Add class to parse Link headers from HTTP responses by GromNaN · Pull Request #60420 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WebLink] Add class to parse Link headers from HTTP responses #60420

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 agre 8000 e to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: 7.3
Choose a base branch
from

Conversation

GromNaN
Copy link
Member
@GromNaN GromNaN commented May 14, 2025
Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Some HTTP API expose a Link header for pagination (See GitHub API or Sentry API), so it's necessary to parse this header to consume the API.

Since we already have a WebLink component, I think it's a good fit to add the logic for parsing the HTTP header into this component.

The existing packages use simplified pattern and does not support all the spec features:

@carsonbot

This comment was marked as outdated.

@carsonbot carsonbot added this to the 7.3 milestone May 14, 2025
@GromNaN GromNaN force-pushed the weblink-parser branch 2 times, most recently from 5a7a0d4 to 4378fb8 Compare May 14, 2025 11:05
@GromNaN
Copy link
Member Author
GromNaN commented May 14, 2025

Thanks for the review @OskarStark, I addressed all your comments.

@GromNaN GromNaN force-pushed the weblink-parser branch 2 times, most recently from 0783d20 to cb63475 Compare May 14, 2025 11:41
Copy link
Member
@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

Nit

*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
final class HttpHeaderParser
Copy link
Member

Choose a reason for hiding this comment

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

final + no base type = not SOLID

Copy link
Member Author

Choose a reason for hiding this comment

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

I blindly replicated the same thing as HttpHeaderSerializer. Let's remove the final.

Copy link
Member

Choose a reason for hiding this comment

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

We should remove final there also then.
BTW, did you consider a service definition to allow autowiring?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed the final from the other class and added framework integration.

Copy link
Member
@stof stof May 15, 2025

Choose a reason for hiding this comment

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

Should we add an interface to provide autowiring based on the interface rather than the concrete implementation ?

Copy link
Member Author

Choose a reason for hiding this comment

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

In absolute terms, that's what we should do. We would do the same for HttpHeaderSerializer then.
But I don't see why anyone would write a different implementation.

$rels = preg_split('/\s+/', trim($params['rel']));
unset($params['rel']);

$link = new Link(array_shift($rels), $href);
8000
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest starting with null instead of array_shift, it should be more performant

@@ -180,6 +180,11 @@ public function getAttributes(): array
return $this->attributes;
}

public function getAttribute(string $attribute): string|\Stringable|int|float|bool|array|null
Copy link
Member

Choose a reason for hiding this comment

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

Please update the documentation of the attributes property to be consistent with the type defined in withAttribute.

It would also be great to document the type of arrays being supported, and to add the precise return type in getAttributes

Copy link
Member
@stof stof May 15, 2025

Choose a reason for hiding this comment

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

maybe this should be a separate PR to merge it in 7.3 though, to provide the proper type sooner.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll remove this new method because it's not part of the interface. Accessing array value with null coalescence if fine.

}
}

if (!isset($params['rel'])) {
Copy link
Member

Choose a reason for hiding this comment

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

what should be done if rel is an array because it is specified multiple times ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I'll take the first value according to the spec:

5.3. Relation Type
The relation type of a link is conveyed in the "rel" parameter's value. The "rel" parameter MUST NOT appear more than once in a given link-value; occurrences after the first MUST be ignored by parsers.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if we should still return links that doesn't have a rel attribute. They will never be returned by getLinksByRel, but give exhaustivity if the parser is used for tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to return links without rel.

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

Successfully merging this pull request may close these issues.

7 participants
0