Fixed a bug in normalizing URL parameters #492
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request fixes #275. The bug is that Scribe uses the
id
as the default URL parameter when no inline binding is applied, but the user might be using another column by overriding thegetRouteKeyName()
method in the model.Behavior
Old behavior
id
.New behavior
getRouteKeyName()
.id
.Examples of the differences can be found in a previous discussion.
Code
Before
Camel\Extraction\ExtractedEndpointData@normalizeResourceParamName
is called in the constructor before calling the parent constructor and initializing the object, so you cannot access themethod
property in the normalization process, as it has not been initialized yet.After
Camel\Extraction\ExtractedEndpointData@normalizeResourceParamName
is called after calling the parent constructor.If there was no inline binding in the route,
Camel\Extraction\ExtractedEndpointData@getFieldBindingForUrlParam
will search for a type-hinted variable whose name matches the route segment name, and will use it to callgetRouteKeyName()
on the model.Testing
Created two feature tests in
Knuckles\Scribe\Tests\GenerateDocumentationTest
to test generating URL params from resource and non-resource routes with model binding. Created some fixtures to achieve that: ATestPost
model that overridesgetRouteKeyName()
to returnslug
, A partial resource controller with an injectedTestPost
argument, and another partial resource controller for a nested resource(posts.users)
with injectedTestPost
andTestUser
models.Possible Refactor
Maybe we can create a new class
Src\Extracting\UrlParamsNormalizer
and move the normalization logic into it. We can use that class inside the constructor ofCamel\Extraction\ExtractedEndpointData
.