Skip to content

Commit

Permalink
Allow empty type from openapi yaml (#420)
Browse files Browse the repository at this point in the history
* Resolves #419
* apply fix in from yaml class instead
* conditionally set type on schema property from yaml
* cleans up structural error in paths: should NOT have additional properties additionalProperty: tags
  • Loading branch information
cnizzardini authored May 25, 2022
1 parent 24dbc2c commit 9e2e405
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Lib/OpenApi/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
public function toArray(): array
{
$vars = get_object_vars($this);
$vars = ArrayUtility::removeKeysMatching($vars, ['resource', 'operations', 'ref']);
$vars = ArrayUtility::removeKeysMatching($vars, ['resource', 'operations', 'ref', 'tags']);

// remove items if null to reduce JSON clutter
$vars = ArrayUtility::removeNullValues($vars, ['summary', 'description']);
Expand Down
8 changes: 5 additions & 3 deletions src/Lib/OpenApi/SchemaProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class SchemaProperty implements JsonSerializable, SchemaInterface
*/
public function __construct(
?string $name = null,
string $type = 'string',
?string $type = null,
?string $format = null,
?string $description = null,
mixed $example = null,
) {
$this->name = $name;
$this->setType($type);
if ($type !== null) {
$this->setType($type);
}
$this->format = $format;
$this->description = $description;
$this->example = $example;
Expand Down Expand Up @@ -91,7 +93,7 @@ public function toArray(): array
[
'format','title','description','multipleOf','minimum','maximum','minLength','maxLength','pattern',
'minItems','maxItems','minProperties','maxProperties','items','enum','default','exclusiveMinimum',
'exclusiveMaximum','uniqueItems','nullable',
'exclusiveMaximum','uniqueItems','nullable','type',
]
);

Expand Down
3 changes: 2 additions & 1 deletion src/Lib/OpenApi/SchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function setType(string $type)
{
if (!in_array($type, OpenApiDataType::TYPES)) {
throw new InvalidArgumentException(
"Type $type is not valid. Must be one of " . implode(',', OpenApiDataType::TYPES)
"Schema type of `$type` is invalid. Must be one of: " .
implode(',', OpenApiDataType::TYPES)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Lib/Schema/SchemaPropertyFromYamlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public function create(string $name, array $yaml): SchemaProperty
$schemaProperty = (new SchemaProperty())
->setName($name)
->setDescription($yaml['description'] ?? '')
->setType($yaml['type'] ?? '')
->setReadOnly($yaml['readonly'] ?? false)
->setWriteOnly($yaml['writeOnly'] ?? false)
->setRequired($yaml['required'] ?? false)
->setEnum($yaml['enum'] ?? [])
->setExample($yaml['example'] ?? '')
->setExample($yaml['example'] ?? null)
->setItems($yaml['items'] ?? [])
->setRefEntity($yaml['$ref'] ?? '')
->setFormat($yaml['format'] ?? '');

$properties = [
'type',
'maxLength',
'minLength',
'pattern',
Expand Down
19 changes: 19 additions & 0 deletions tests/test_app/config/swagger-with-existing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,24 @@ components:
example: error message here
xml:
name: response
# issue: https://github.com/cnizzardini/cakephp-swagger-bake/issues/419
Address:
type: object
properties:
city:
type: string
example: "Brazzaville"
zone:
type: string
example: "Zone 1"
User:
type: object
properties:
name:
type: string
example: "De claude"
address:
$ref: '#/components/schemas/Address'
# end issue
security:
- BearerAuth: []

0 comments on commit 9e2e405

Please sign in to comment.