-
Notifications
You must be signed in to change notification settings - Fork 70
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
Added schema definitions, responses, support to JWT auth, Swagger UI and Docker environment #24
Merged
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
1b7f7b2
chore: adds docker container with php7.4
aa4fe6c
feat: adds generator to swagger definitions section
d9f6b04
style: removes extra "\" from test routes definitions
0d8fe42
feat: changes php version according to travis ci definitions
d257c3b
refactor: adds action extraction methods to Route class
605958d
refactor: obtains model relations from helper instead of class
fb2482a
refactor: disables generate definition example
71c1b4f
refactor: changes model extraction from Generator to Route
9ae6f11
feat: adds generator to success and error responses
833a448
feat: adds definitions and responses to generated docs
ba07e5a
docs: adds docs to generate definitions and responses
6c82814
docs: updates docs details
805dddd
feat: adds factory to generate example field on parameters.
947efec
feat: fixes form request name class extraction
f979832
feat: adds view to show swagger docs
ebce3da
feat: adds swagger ui dependency
23e6c82
feat: adds swagger ui route and view
7d6aec3
test: adds tests to GenerateSwaggerDocCommand
75d59ad
refactor: changes to filter routes before generate docs
5e11250
docs: changes generate swagger command usage
e6ee2bc
feat: adds routes versions to swagger ui view
36ddef3
feat: adds select to choose api version on Swagger UI
c90c3fc
feat: adds method to route return form request from params
ed5a2ad
feat: adds errors response generation with laravel defaults responses
2029b79
feat: adds cast to array on get throws from route docblock
bd7637c
feat: adds handlers to generate definitions to error responses
c7d5e4f
fix: fixes to create one definition for each form request
404da85
chore: adds xdebug to Dockerfile
1a8e175
test: adds tests to return of getAppends method
8f825f0
feat: throws exception when response has no definition configured
56a4658
test: adds test when find not existent version
f82c559
feat: adds swagger file name generator to make it more automatic
0c5979c
fix: fixes definition generation when has no model defined
91ed0b5
style: fixes return type from getModel method
f109f0c
fix: fixes check model on success response generator
1ac2f44
feat: adds jwt security definitions generator
2a2cfb6
fix: fixes to get all middleware registered for action
29d3b44
test: adds test for route
3e11718
fix: removes output parameter from command
81365ca
feat: add trait to use when the model has appends
83492bc
docs: adds info and examples how to use the laravel swagger
ea54419
test: fixes passport routes calling
5716e27
chore: adds docker-sync to improve performance on mac
sfelix-martins a99a9d4
chore: adds gitattributes
sfelix-martins 8d87fee
feat: changes parseSecurity and parseDocsBlock from version to global
sfelix-martins 5e25242
feat: removes param --all-versions from command
sfelix-martins ea2a76e
refactor: changes the model annotation obtaining
sfelix-martins 344187d
refactor: changes getActionClassInstance to use action from $this
sfelix-martins b8ee2e7
refactor: removes alias to check if can generate definitions
sfelix-martins c6e83a0
refactor: removes not required filter to check if allows generate
sfelix-martins 2964244
conflict fix
mtrajano bad6685
moved configs around for better versioning
mtrajano 316215d
more clean up and tests
mtrajano 46a4254
more fixes and tests
mtrajano 0ab8825
relations logic do not call method before checking return typehint
mtrajano dd5e5e6
test: changes to assert example is not empty instead of just null
sfelix-martins 9f8aa0b
config for parsing relationships and example data, disable by default
mtrajano 511eda6
Merge branch 'master' of github.com:coopersystem-fsd/laravel-swagger …
mtrajano 428989e
fixed documentation
mtrajano 77b52ce
skipping flaky tests for now
mtrajano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,88 +38,43 @@ function get_annotations(string $from, string $annotationName): array | |
/** | ||
* Identify all relationships for a given model | ||
* | ||
* @todo Create unit test fot this method. | ||
* @param Model $model Model | ||
* @param string $heritage A flag that indicates whether parent and/or child | ||
* relationships should be included | ||
* @return array | ||
* @throws ReflectionException | ||
*/ | ||
function get_all_model_relations(Model $model = null, $heritage = 'all') { | ||
$modelName = get_class($model); | ||
function get_all_model_relations(Model $model, $heritage = 'all'): array { | ||
$types = ['children' => 'Has', 'parents' => 'Belongs', 'all' => '']; | ||
$heritage = in_array($heritage, array_keys($types)) ? $heritage : 'all'; | ||
|
||
$filter = $types[$heritage] ?? $types['all']; | ||
$reflectionClass = new ReflectionClass($model); | ||
$traits = $reflectionClass->getTraits(); | ||
$traitMethodNames = []; | ||
foreach ($traits as $name => $trait) { | ||
$traitMethods = $trait->getMethods(); | ||
foreach ($traitMethods as $traitMethod) { | ||
$traitMethodNames[] = $traitMethod->getName(); | ||
} | ||
} | ||
|
||
// Checking the return value actually requires executing the method. | ||
// So use this to avoid infinite recursion. | ||
$currentMethod = collect(explode('::', __METHOD__))->last(); | ||
$filter = $types[$heritage]; | ||
// The method must be public | ||
$methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC); | ||
|
||
$methods = collect($methods) | ||
->filter( | ||
function (ReflectionMethod $method) use ( | ||
$modelName, | ||
$traitMethodNames, | ||
$currentMethod | ||
) { | ||
$methodName = $method->getName(); | ||
/** | ||
* - The method must not originate in a trait | ||
* - It must not be a magic method | ||
* - It must be in the self scope and not inherited | ||
* - It must be in the this scope and not static | ||
* - It must not be an override of this one | ||
*/ | ||
if (!in_array($methodName, $traitMethodNames) | ||
&& strpos($methodName, '__') !== 0 | ||
&& $method->class === $modelName | ||
&& !$method->isStatic() | ||
&& $methodName != $currentMethod | ||
) { | ||
$parameters = ( | ||
new ReflectionMethod($modelName, $methodName) | ||
)->getParameters(); | ||
// If required parameters exist, this will be false and | ||
// omit this method | ||
return collect($parameters)->filter( | ||
function (ReflectionParameter $parameter) { | ||
// The method must have no required parameters | ||
return !$parameter->isOptional(); | ||
} | ||
)->isEmpty(); | ||
} | ||
return false; | ||
->map(function (ReflectionMethod $method) use ($filter, $model) { | ||
|
||
if (!$method->getReturnType() || !$method->getReturnType()->getName()) { | ||
return null; | ||
} | ||
) | ||
->map(function (ReflectionMethod $method) use ($model, $filter) { | ||
|
||
$methodName = $method->getName(); | ||
/** @var Relation|mixed $relation */ | ||
$relation = $model->$methodName(); | ||
$returnTypeClass = $method->getReturnType()->getName(); | ||
|
||
// Must return a Relation child. This is why we only want to do | ||
// this once | ||
if (is_subclass_of($relation, Relation::class)) { | ||
$type = (new ReflectionClass($relation))->getShortName(); | ||
if (is_subclass_of($returnTypeClass, Relation::class)) { | ||
$relation = $method->invoke($model); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still not ideal but slightly safer than before. It at least checks the return typehint of the method before invoking it. Am also going to add a config to disable this by default and add a warning on the docs that this will invoke methods in the model to get the relation name. |
||
|
||
// If relation is of the desired heritage | ||
if (!$filter || strpos($type, $filter) === 0) { | ||
if (!$filter || strpos($returnTypeClass, $filter) === 0) { | ||
return [ | ||
'method' => $methodName, | ||
'related_model' => $relation->getRelated(), | ||
'relation' => get_class($relation), | ||
]; | ||
} | ||
} | ||
|
||
return null; | ||
}) | ||
// Remove elements reflecting methods that do not have the desired | ||
|
Oops, something went wrong.
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.
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.
Make is much safer, only creates an instance without making a connection to the db
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 know if it's more safer. Please, think about cases like that:
If think we should to use transactions to avoid surprises...
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 checked here other reasons to use
create
instead ofmake
. We done it to generate theexample
data to timestamps and id fields too. I don't know if it sufficient... bus was one of the reason.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 test that check the
example
response is checking just if the result is not null. But I think that the correct behavior should check if is not empty. If apply this change the build will fail with themake
method, because empty values increated_at
,updated_at
naid
properties.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.
Ahh good point on the
user_id
example, I guess we can't avoid a transaction here. I'll change this back to what it was. Reasons I was trying to avoid create even with the transaction are 1. to avoid having to make a connection to the db to generate static data, 2. it doesn't necessarily leave the db in an unchanged state (one example being the AUTO_INCREMENT value that remains unchanged even with a rollback) so it might just be some extra side effects that the user might not be expecting when running this. We might also want to add a config to disable this part by default and let the user manually turn it on if he chooses to, along with some prominent warnings not to run this in a prod environment (which should be obvious but we can't always guarantee that).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 allow. I think we can maybe too ask user a different DB connection just to generate the docs... what about it?