-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Fix silencing errors in extension handling. #12586
Conversation
src/Http/Response.php
Outdated
* | ||
* This is needed for RequestHandlerComponent and recognition of types. | ||
* | ||
* @param string $type |
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.
Missing parameter comment
src/Http/Response.php
Outdated
* This is needed for RequestHandlerComponent and recognition of types. | ||
* | ||
* @param string $type | ||
* @param string|array $definition |
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.
Missing parameter comment
src/Http/Response.php
Outdated
* @param string|array $definition Definition of the content type. | ||
* @return void | ||
*/ | ||
public function setType($type, $definition) |
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.
Why do we need setType()
when there is withType()
?
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.
See the ticket referenced. The type() deprecation lost a way to set new type definitions.
As such RequestHandler would just not be able to serve those anymore without calling
$this->response->setType('ical', 'my/ical');
etc
This has nothing to do with withType() response modification. This is only for setting/adding/replacing a (new) type into the map.
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.
Okay, in that case addType()
would be a more intuitive name since it's adds/maps a new type, doesn't actually set type for the response.
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.
I thought about it. add() kind of implies that you could also add a type into existing mappings.
But that is not the case, this replaces existing ones completely. As such I thought set() would be more intuitive and correct.
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.
I don't think the fact that addType()
can overwrite existing mapping will cause confusion to anymore. Someone requiring to overwrite existing type mapping is unlikely anyway. mapType()
would have been the ideal name but method with that name already exists for another purpose 🙁.
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.
How about the name setTypeMap()
?
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.
Or setTypeMapDefinition().
Modified, also added tests for it.
src/Http/Response.php
Outdated
* This is needed for RequestHandlerComponent and recognition of types. | ||
* | ||
* @param string $type Content type. | ||
* @param string|array $definition Definition of the content type. |
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.
$mimeType
/$contentType
would be a more intuitive variable name.
Always raising a 404 on unknown 'extensions' also means that an application can never have |
OK. Then this current change is still valid, as I didnt touch the non-configured ones. But IMO there shouldn't be any such |
Tests should be added to give a better idea of how exactly this change impacts the handing of URLs with dot in last segment. |
The existing tests also fail, how would we adjust those here? |
@@ -594,6 +598,9 @@ public function renderAs(Controller $controller, $type, array $options = []) | |||
$viewClass = null; | |||
if ($builder->getClassName() === null) { | |||
$viewClass = App::className($view, 'View', 'View'); | |||
if ($viewClass === false) { | |||
throw new RuntimeException('Configured view class can not be found: ' . $view); | |||
} |
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.
Won't this break use cases where extensions are using sub-directories to render their views and not a custom view class?
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.
The tests failures will go away if this particular change is reverted.
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.
Should we only fail if you actually configure to a specific className? I feel like silently doing nothing here prevents this developer mistake from being visible.
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.
Not sure how will you decide if it's a dev error. As @markstory pointed out one can just use View
class to render content of any string type by relying on templates in sub directories and just set appropriate content/mime type for response instance.
Is your statement for when an extension is enabled in routing? If |
|
Is it adjusted as per your feedback now? |
Shouldn't new test be added for change in RequestHandler? |
Added a test case and also removed the deprecated initialize() call (as it didnt do anything anyway). |
public function testUnrecognizedExtensionFailure() | ||
{ | ||
Router::extensions(['json', 'foo'], false); | ||
$this->Controller->request = $this->Controller->request->withParam('_ext', 'foo'); |
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.
Using setRequest()/getRequest() instead of the $request property will save use effort when doing 3.next/4.x merges :)
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.
You want me to touch the code again? :)
All done. |
* @param string|array $mimeType Definition of the mime type. | ||
* @return void | ||
*/ | ||
public function setTypeMap($type, $mimeType) |
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.
Is this method setting a map or just a single type? The name and implementation don't appear to match.
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.
A single one. Is setTypeToMap() or setTypeIntoMap() better?
I didnt want to addType() as it would also overwrite the existing one.
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.
Hmm. On second thought I don't have a better name that isn't long.
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.
setTypeMap()
suggests that it sets whole map of types, not single type.
Main keyword here is map. After look at dictionary, I think it could be mapType()
, associateType()
, linkType()
.
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.
I like mapType() but that one is already taken! To not confuse people I would stick to the method.
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.
I also don't think setTypeMap()
for setting one tpye within the map is a good name.
The best would really be mapType()
, unfortunately.
How about setTypeMapped()
or setMappedType()
?
Or updateType()
?
associateType()
, isn't bad either.
src/Http/Response.php
Outdated
/** | ||
* Sets a content type definition into the map. | ||
* | ||
* E.g.: setType('xhtml' => ['application/xhtml+xml', 'application/xhtml']) |
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.
Invalid syntax. Uses wrong function name.
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.
Fixed
Are you ok with this as is? Or do I have to use a different method name like updateTypeMap() etc? |
Out of curiosity I setup a vanilla CakePHP and and tested some cases. CakePHP Core 3.6.12 Implemented
|
Can u open that up as a separate ticket. This should not get pickybacked on this one. |
@dereuromark thought it was relevant. Anyway, done so in #12624 |
This fixes most of the issues from #12585
It does however not fix the silencing of non configured extensions.
So if you didnt whitelist
ical
and then just accessed/controller/action.ical
it would just silently serve the content of/controller/action
without any indication of problems. One would except a 404 here, though.The reason for this last issue seems to be that in such a case routing just ignores the ext, and sets it to null for going into the controller. That's bad.