Skip to content

Added support for readonly/writeonly properties #81

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 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/OpenAPIFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function mockRequest(
throw NoRequest::forPathAndMethodAndContentType($path, $method, $contentType);
}

return (new SchemaFaker($contents[$contentType]->schema, $this->options))->generate();
return (new SchemaFaker($contents[$contentType]->schema, $this->options, true))->generate();
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/SchemaFaker/ObjectFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ObjectFaker
/**
* @return array<mixed>
*/
public static function generate(Schema $schema, Options $options): array
public static function generate(Schema $schema, Options $options, bool $request = false): array
{
$result = [];

Expand All @@ -33,6 +33,12 @@ public static function generate(Schema $schema, Options $options): array
$allPropertyKeys = array_merge($requiredKeys, $selectedOptionalKeys);

foreach ($schema->properties as $key => $property) {
if ($property instanceof Schema) {
if (($request && $property->readOnly) || (! $request && $property->writeOnly)) {
continue;
}
}

if (! $options->getAlwaysFakeOptionals() && ! in_array($key, $allPropertyKeys, true)) {
continue;
}
Expand Down
6 changes: 4 additions & 2 deletions src/SchemaFaker/SchemaFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ final class SchemaFaker
{
private Schema $schema;
private Options $options;
private bool $request;

public function __construct(Schema $schema, Options $options)
public function __construct(Schema $schema, Options $options, bool $request = false)
{
$schemaData = json_decode(json_encode($schema->getSerializableData()), true);
$this->schema = new Schema($this->resolveOfConstraints($schemaData));
$this->options = $options;
$this->request = $request;
}

/**
Expand All @@ -41,7 +43,7 @@ public function generate()
}

if ($this->schema->type === 'object') {
return ObjectFaker::generate($this->schema, $this->options);
return ObjectFaker::generate($this->schema, $this->options, $this->request);
}

if ($this->schema->type === 'string') {
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/SchemaFaker/ObjectFakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,64 @@ function it_can_fake_all_properties_if_always_fake_optionals_option_is_set()

$this->assertMatchesJsonSnapshot($fakeData);
}

/** @test */
function it_does_not_inlcude_readonly_properties_when_type_is_request()
{
$yaml = <<<YAML
type: object
properties:
id:
type: integer
readOnly: true
username:
type: string
password:
type: string
writeOnly: true
required:
- id
- username
- password
YAML;

$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options, true);

self::assertIsArray($fakeData);
self::assertArrayNotHasKey('id', $fakeData);
self::assertArrayHasKey('username', $fakeData);
self::assertArrayHasKey('password', $fakeData);

$this->assertMatchesJsonSnapshot($fakeData);
}

/** @test */
function it_does_not_inlcude_writeonly_properties_when_type_is_response()
{
$yaml = <<<YAML
type: object
properties:
id:
type: integer
readOnly: true
username:
type: string
password:
type: string
writeOnly: true
required:
- id
- username
- password
YAML;

$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options);

self::assertIsArray($fakeData);
self::assertArrayHasKey('id', $fakeData);
self::assertArrayHasKey('username', $fakeData);
self::assertArrayNotHasKey('password', $fakeData);

$this->assertMatchesJsonSnapshot($fakeData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "laborum",
"password": "in"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 488690146,
"username": "in"
}