Skip to content

Resolve: Consider OpenAPI spec examples in faker code generation #20 #21

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2f9aa2e
Initial commit to create a PR
SOHELAHMED7 Aug 1, 2024
fa7c240
Implement for multiple data types
SOHELAHMED7 Aug 2, 2024
c0405e8
Implement faker generation of array - WIP
SOHELAHMED7 Aug 6, 2024
701d612
WIP 2
SOHELAHMED7 Aug 8, 2024
5b87057
Implement for object and obj with array & nested object - WIP
SOHELAHMED7 Aug 8, 2024
46b8d48
Implement for object and obj with nested array & nested object
SOHELAHMED7 Aug 9, 2024
c1bc831
Implement faker generation for array of referenced component schema
SOHELAHMED7 Aug 9, 2024
2f08c41
Implement oneOf and refactor - WIP
SOHELAHMED7 Aug 13, 2024
9f83e78
Implement oneOf and refactor - WIP 2
SOHELAHMED7 Aug 13, 2024
b3c660b
Implement oneOf and refactor - WIP 3
SOHELAHMED7 Aug 13, 2024
1e7cea6
Complex oneOf - WIP
SOHELAHMED7 Aug 13, 2024
14b2146
Implement complex oneOf + fix error 'Creating default object from emp…
SOHELAHMED7 Aug 14, 2024
4e6bc03
Fix failing test for x_db_type
SOHELAHMED7 Aug 14, 2024
b2c21d1
Fix bug
SOHELAHMED7 Aug 14, 2024
52f2320
Fix failing test
SOHELAHMED7 Aug 14, 2024
b037265
Fix failing test
SOHELAHMED7 Aug 15, 2024
0910073
Fix errors
SOHELAHMED7 Aug 15, 2024
f0c5cd3
Fix errors 2 + implement custom attribute `x-no-relation`
SOHELAHMED7 Aug 15, 2024
de75ca7
Fix failing test in PHP >= 8.1
SOHELAHMED7 Aug 16, 2024
6ab30cc
Fix count issue for nested array
SOHELAHMED7 Aug 16, 2024
960cc43
Add typehint to fn args
SOHELAHMED7 Aug 16, 2024
4f6d728
Fix issues + add support for all refs only in oneOf
SOHELAHMED7 Aug 16, 2024
65625f0
Refactor
SOHELAHMED7 Aug 17, 2024
77f6ff2
Fix bug
SOHELAHMED7 Aug 17, 2024
ee6f7ec
Add docs for `x-no-relation` and create its constant
SOHELAHMED7 Aug 17, 2024
6cfadcf
Handle example
SOHELAHMED7 Aug 17, 2024
b3023c4
Add test spec file
SOHELAHMED7 Aug 17, 2024
775ff26
Enhance docs
SOHELAHMED7 Aug 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix errors
  • Loading branch information
SOHELAHMED7 committed Aug 15, 2024
commit 091007395cd5dffce14971a4e7ae901b27c32efb
3 changes: 2 additions & 1 deletion src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ protected function resolveProperty(
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
->setNullable($nullableValue)
->setIsPrimary($property->isPrimaryKey())
->setForeignKeyColumnName($property->fkColName);
->setForeignKeyColumnName($property->fkColName)
->setFakerStub($this->guessFakerStub($attribute, $property));
if ($property->isReference()) {
if ($property->isVirtual()) {
throw new InvalidDefinitionException('References not supported for virtual attributes');
Expand Down
18 changes: 14 additions & 4 deletions src/lib/FakerStubResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function fakeForObject(SpecObjectInterface $items): string
foreach ($items->properties as $name => $prop) {
/** @var SpecObjectInterface $prop */

if ($prop->properties) { // object
if ($prop->properties) { // nested object
$result = $this->{__FUNCTION__}($prop);
} else {
$ps = new PropertySchema($prop, $name, $cs);
Expand Down Expand Up @@ -387,21 +387,31 @@ public function arbitraryArray(): string

public function aElementFaker($data): ?string
{
$aElementData = Json::decode(Json::encode($data));
$aElementData = Json::decode(Json::encode($data)); // object of stdClass -> array
$compoSchemaData = [
'properties' => [
'unnamedProp' => $aElementData['items']
]
];

$schema = new Schema($compoSchemaData);
$cs = new ComponentSchema($schema, 'UnnamedCompo');
if ($this->config) {
$rc = new ReferenceContext($this->config->getOpenApi(), Yii::getAlias($this->config->openApiPath));
$schema->setReferenceContext($rc);
}

$cs = new ComponentSchema($schema, 'UnnamedCompo');
$dbModels = (new AttributeResolver('UnnamedCompo', $cs, new JunctionSchemas([]), $this->config))->resolve();

foreach ($schema->properties as $name => $prop) {
if($prop->items instanceof Reference) {
$dbModels->attributes[$name] = new Attribute($name, [
'phpType' => 'array',
'dbType' => 'array',
'reference' => $prop->items->getReference(),
]);
}
}

return (new static($dbModels->attributes['unnamedProp'], $cs->getProperty('unnamedProp'), $this->config))->resolve();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ components:
name:
type: string
example: cat
# example: ['long-tail', 'short-tail', 'black', 'white']
age:
type: integer
example: 2
Expand All @@ -48,8 +47,7 @@ components:
minItems: 6
maxItems: 10
uniqueItems: true
# type: string
# example: ['long-tail', 'short-tail', 'black', 'white']
# example: ['long-tail', 'short-tail', 'black', 'white']
number_arr:
type: array
items:
Expand Down
Loading