Skip to content

Commit 9bd5b17

Browse files
Added support for readonly/writeonly properties
1 parent 7c711d7 commit 9bd5b17

6 files changed

+80
-4
lines changed

Diff for: src/OpenAPIFaker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function mockRequest(
8484
throw NoRequest::forPathAndMethodAndContentType($path, $method, $contentType);
8585
}
8686

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

9090
/**

Diff for: src/SchemaFaker/ObjectFaker.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class ObjectFaker
2222
/**
2323
* @return array<mixed>
2424
*/
25-
public static function generate(Schema $schema, Options $options): array
25+
public static function generate(Schema $schema, Options $options, bool $request = false): array
2626
{
2727
$result = [];
2828

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

3535
foreach ($schema->properties as $key => $property) {
36+
if ($property instanceof Schema) {
37+
if (($request && $property->readOnly) || (! $request && $property->writeOnly)) {
38+
continue;
39+
}
40+
}
41+
3642
if (! $options->getAlwaysFakeOptionals() && ! in_array($key, $allPropertyKeys, true)) {
3743
continue;
3844
}

Diff for: src/SchemaFaker/SchemaFaker.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ final class SchemaFaker
2323
{
2424
private Schema $schema;
2525
private Options $options;
26+
private bool $request;
2627

27-
public function __construct(Schema $schema, Options $options)
28+
public function __construct(Schema $schema, Options $options, bool $request = false)
2829
{
2930
$schemaData = json_decode(json_encode($schema->getSerializableData()), true);
3031
$this->schema = new Schema($this->resolveOfConstraints($schemaData));
3132
$this->options = $options;
33+
$this->request = $request;
3234
}
3335

3436
/**
@@ -41,7 +43,7 @@ public function generate()
4143
}
4244

4345
if ($this->schema->type === 'object') {
44-
return ObjectFaker::generate($this->schema, $this->options);
46+
return ObjectFaker::generate($this->schema, $this->options, $this->request);
4547
}
4648

4749
if ($this->schema->type === 'string') {

Diff for: tests/Unit/SchemaFaker/ObjectFakerTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,64 @@ function it_can_fake_all_properties_if_always_fake_optionals_option_is_set()
135135

136136
$this->assertMatchesJsonSnapshot($fakeData);
137137
}
138+
139+
/** @test */
140+
function it_does_not_inlcude_readonly_properties_when_type_is_request()
141+
{
142+
$yaml = <<<YAML
143+
type: object
144+
properties:
145+
id:
146+
type: integer
147+
readOnly: true
148+
username:
149+
type: string
150+
password:
151+
type: string
152+
writeOnly: true
153+
required:
154+
- id
155+
- username
156+
- password
157+
YAML;
158+
159+
$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options, true);
160+
161+
self::assertIsArray($fakeData);
162+
self::assertArrayNotHasKey('id', $fakeData);
163+
self::assertArrayHasKey('username', $fakeData);
164+
self::assertArrayHasKey('password', $fakeData);
165+
166+
$this->assertMatchesJsonSnapshot($fakeData);
167+
}
168+
169+
/** @test */
170+
function it_does_not_inlcude_writeonly_properties_when_type_is_response()
171+
{
172+
$yaml = <<<YAML
173+
type: object
174+
properties:
175+
id:
176+
type: integer
177+
readOnly: true
178+
username:
179+
type: string
180+
password:
181+
type: string
182+
writeOnly: true
183+
required:
184+
- id
185+
- username
186+
- password
187+
YAML;
188+
189+
$fakeData = ObjectFaker::generate(SchemaFactory::fromYaml($yaml), $this->options);
190+
191+
self::assertIsArray($fakeData);
192+
self::assertArrayHasKey('id', $fakeData);
193+
self::assertArrayHasKey('username', $fakeData);
194+
self::assertArrayNotHasKey('password', $fakeData);
195+
196+
$this->assertMatchesJsonSnapshot($fakeData);
197+
}
138198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"username": "laborum",
3+
"password": "in"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": 488690146,
3+
"username": "in"
4+
}

0 commit comments

Comments
 (0)