Skip to content

Commit

Permalink
Added Required to the Schema for rendering. (#3)
Browse files Browse the repository at this point in the history
The `Required` option does not seem to be rendering for a property. I am
not sure if this is intentional or not. This PR adds the support for it
to be rendered out with `required` being defaulted to true. This allows
`required` properties to be marked as such.
  • Loading branch information
robert-sykes authored Jan 17, 2024
1 parent e6f63d6 commit eb8f29a
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/Attribute/AbstractProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class AbstractProperty
public function __construct(
public string $name,
public string $description,
public bool $required,
) {
}

Expand All @@ -17,6 +18,7 @@ public function toArray(): array
return [
'name' => $this->name,
'description' => $this->description,
'required' => $this->required,
];
}
}
4 changes: 2 additions & 2 deletions src/Attribute/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function __construct(
public readonly PropertyType $type = PropertyType::STRING,
public readonly ?Format $format = null,
public readonly ?string $example = null,
public readonly bool $required = true,
bool $required = true,
) {
parent::__construct($name, $description);
parent::__construct($name, $description, $required);
}

public function toArray(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/PropertyArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function __construct(
string $description = '',
public readonly ?Format $format = null,
public readonly ?string $example = null,
public readonly bool $required = true,
bool $required = true,
) {
parent::__construct($name, $description);
parent::__construct($name, $description, $required);
}

public function toArray(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/PropertyArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function __construct(
string $description = '',
public ?Format $format = null,
public ?string $example = null,
public bool $required = true,
bool $required = true,
) {
parent::__construct($name, $description);
parent::__construct($name, $description, $required);
}

public function toArray(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/PropertyEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function __construct(
string $description = '',
public ?Format $format = null,
public ?string $example = null,
public bool $required = true,
bool $required = true,
) {
parent::__construct($name, $description);
parent::__construct($name, $description, $required);
}

public function toArray(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/PropertyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public function __construct(
public readonly string $class,
string $description = '',
public readonly array $items = [],
public readonly bool $required = true,
bool $required = true,
) {
parent::__construct($name, $description);
parent::__construct($name, $description, $required);
}

public function toArray(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/JsonGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function __construct(
*/
public function generate(): string
{
return json_encode($this->generator->generate(), JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
return json_encode($this->generator->generate(), JSON_THROW_ON_ERROR);
}
}
6 changes: 6 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Schema
public function render(array $document): array
{
$properties = [];
$required = [];

foreach ($document['properties'] as $property) {
$properties[$property['name']]['type'] = PropertyTypeTranslator::translate($property['type']);
Expand All @@ -24,12 +25,17 @@ public function render(array $document): array
if (isset($property['example'])) {
$properties[$property['name']]['example'] = $property['example'];
}

if (isset($property['required'])) {
$required[] = $property['name'];
}
}

$message[$document['name']] = [
'payload' => [
'type' => 'object',
'properties' => $properties,
'required' => $required,
],
];

Expand Down
50 changes: 47 additions & 3 deletions tests/Integration/DumpSpecificationConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function testExecuteClass(): void
description: 'Is user a citizen'
format: boolean
example: 'true'
required:
- name
- email
- age
- isCitizen
YAML;
Expand Down Expand Up @@ -124,6 +129,11 @@ public function testExecuteYaml(): void
description: 'Is user a citizen'
format: boolean
example: 'true'
required:
- name
- email
- age
- isCitizen
PaymentExecuted:
payload:
type: object
Expand All @@ -138,6 +148,9 @@ public function testExecuteYaml(): void
description: 'Creation date'
format: date-time
example: '2023-11-23 13:41:21'
required:
- amount
- createdAt
ProductCreated:
payload:
type: object
Expand Down Expand Up @@ -170,6 +183,16 @@ public function testExecuteYaml(): void
tags:
type: string
description: ''
required:
- id
- amount
- currency
- isPaid
- createdAt
- week
- payment
- products
- tags
YAML;
Expand Down Expand Up @@ -269,7 +292,13 @@ public function testExecuteJson(): void
"format": "boolean",
"example": "true"
}
}
},
"required": [
"name",
"email",
"age",
"isCitizen"
]
}
},
"PaymentExecuted": {
Expand All @@ -288,7 +317,11 @@ public function testExecuteJson(): void
"format": "date-time",
"example": "2023-11-23 13:41:21"
}
}
},
"required": [
"amount",
"createdAt"
]
}
},
"ProductCreated": {
Expand Down Expand Up @@ -332,7 +365,18 @@ public function testExecuteJson(): void
"type": "string",
"description": ""
}
}
},
"required": [
"id",
"amount",
"currency",
"isPaid",
"createdAt",
"week",
"payment",
"products",
"tags"
]
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Attribute/PropertyArrayObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testToArray(): void
'type' => 'array',
'format' => null,
'example' => null,
'required' => true,
];

$actual = $property->toArray();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Attribute/PropertyArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testToArray(): void
'itemsType' => 'string',
'format' => null,
'example' => null,
'required' => true,
];

$actual = $property->toArray();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Attribute/PropertyEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testToArrayOnBackedEnum(): void
'enum' => [1, 2, 3, 4, 5, 6, 7],
'format' => null,
'example' => null,
'required' => true,
];

$actual = $property->toArray();
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Attribute/PropertyObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testToArray(): void
'description' => '',
'type' => 'object',
'items' => [],
'required' => true,
];

$actual = $property->toArray();
Expand All @@ -47,8 +48,10 @@ class: Payment::class,
'type' => 'string',
'format' => null,
'example' => null,
'required' => true,
]
],
'required' => true,
];

$actual = $property->toArray();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Attribute/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testToArray(): void
'type' => 'string',
'format' => null,
'example' => null,
'required' => true,
];

$actual = $property->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,31 @@ public function testUserSignedUp(): void
'description' => 'Name of the user',
'example' => 'John',
'format' => 'string',
'required' => true,
],
[
'name' => 'email',
'type' => 'string',
'description' => 'Email of the user',
'format' => 'email',
'example' => '[email protected]',
'required' => true,
],
[
'name' => 'age',
'type' => 'integer',
'description' => 'Age of the user',
'format' => 'int32',
'example' => '18',
'required' => true,
],
[
'name' => 'isCitizen',
'type' => 'boolean',
'description' => 'Is user a citizen',
'format' => 'boolean',
'example' => 'true',
'required' => true,
],
],
];
Expand All @@ -70,34 +74,39 @@ public function testProductCreated(): void
'type' => 'integer',
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'amount',
'description' => '',
'type' => 'number',
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'currency',
'description' => '',
'type' => 'string',
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'isPaid',
'description' => '',
'type' => 'boolean',
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'createdAt',
'description' => '',
'type' => 'string',
'format' => 'date-time',
'example' => null,
'required' => true,
],
[
'name' => 'week',
Expand All @@ -106,19 +115,22 @@ public function testProductCreated(): void
'enum' => [1, 2, 3, 4, 5, 6, 7],
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'payment',
'description' => '',
'type' => 'object',
'items' => [],
'required' => true,
],
[
'name' => 'products',
'description' => '',
'type' => 'array',
'format' => null,
'example' => null,
'required' => true,
],
[
'name' => 'tags',
Expand All @@ -127,6 +139,7 @@ public function testProductCreated(): void
'format' => null,
'example' => null,
'itemsType' => 'string',
'required' => true,
],
],
];
Expand Down
Loading

0 comments on commit eb8f29a

Please sign in to comment.