Skip to content

Commit

Permalink
chore: stop rendering empty fields (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror authored Jan 29, 2024
1 parent d29a77f commit a1b28e8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/Schema/V2/MessageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public function render(array $document): array
foreach ($document['properties'] as $property) {
$properties[$property['name']]['type'] = PropertyTypeTranslator::translate($property['type']);

if (isset($property['description'])) {
if (!empty($property['description'])) {
$properties[$property['name']]['description'] = $property['description'];
}

if (isset($property['format'])) {
if (!empty($property['format'])) {
$properties[$property['name']]['format'] = $property['format'];
}

if (isset($property['example'])) {
if (!empty($property['example'])) {
$properties[$property['name']]['example'] = $property['example'];
}

if (isset($property['required'])) {
if (isset($property['required']) && $property['required']) {
$required[] = $property['name'];
}
}
Expand Down
34 changes: 8 additions & 26 deletions tests/Integration/DumpSpecificationConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,32 +157,23 @@ public function testExecuteYaml(): void
properties:
id:
type: integer
description: ''
amount:
type: number
description: ''
currency:
type: string
description: ''
isPaid:
type: boolean
description: ''
createdAt:
type: string
description: ''
format: date-time
week:
type: integer
description: ''
payment:
type: string
description: ''
products:
type: string
description: ''
tags:
type: string
description: ''
required:
- id
- amount
Expand Down Expand Up @@ -329,41 +320,32 @@ public function testExecuteJson(): void
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": ""
"type": "integer"
},
"amount": {
"type": "number",
"description": ""
"type": "number"
},
"currency": {
"type": "string",
"description": ""
"type": "string"
},
"isPaid": {
"type": "boolean",
"description": ""
"type": "boolean"
},
"createdAt": {
"type": "string",
"description": "",
"format": "date-time"
},
"week": {
"type": "integer",
"description": ""
"type": "integer"
},
"payment": {
"type": "string",
"description": ""
"type": "string"
},
"products": {
"type": "string",
"description": ""
"type": "string"
},
"tags": {
"type": "string",
"description": ""
"type": "string"
}
},
"required": [
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Schema/V2/MessageRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,50 @@ public function testAttributes(): void

$this->assertEquals($expectedSpecification, Yaml::dump($specification, 10, 2));
}

public function testDoesNotRenderEmptyData(): void
{
$document = [
'name' => 'UserSignedUp',
'properties' => [
[
'name' => 'firstName',
'type' => 'string',
'description' => '',
'example' => '',
'format' => null,
],
[
'name' => 'secondName',
'type' => 'string',
'description' => '',
'example' => '',
'format' => '',
],
],
];

$schema = new MessageRenderer();

$actual = $schema->render($document);

$expected = [
'UserSignedUp' => [
'payload' => [
'type' => 'object',
'properties' => [
'firstName' => [
'type' => 'string',
],
'secondName' => [
'type' => 'string',
],
],
'required' => [],
],
],
];

$this->assertEquals($expected, $actual);
}
}
25 changes: 8 additions & 17 deletions tests/Unit/Symfony/Controller/JsonSpecificationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,41 +139,32 @@ public function test(): void
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": ""
"type": "integer"
},
"amount": {
"type": "number",
"description": ""
"type": "number"
},
"currency": {
"type": "string",
"description": ""
"type": "string"
},
"isPaid": {
"type": "boolean",
"description": ""
"type": "boolean"
},
"createdAt": {
"type": "string",
"description": "",
"format": "date-time"
},
"week": {
"type": "integer",
"description": ""
"type": "integer"
},
"payment": {
"type": "string",
"description": ""
"type": "string"
},
"products": {
"type": "string",
"description": ""
"type": "string"
},
"tags": {
"type": "string",
"description": ""
"type": "string"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,23 @@ public function test(): void
properties:
id:
type: integer
description: ''
amount:
type: number
description: ''
currency:
type: string
description: ''
isPaid:
type: boolean
description: ''
createdAt:
type: string
description: ''
format: date-time
week:
type: integer
description: ''
payment:
type: string
description: ''
products:
type: string
description: ''
tags:
type: string
description: ''
required:
- id
- amount
Expand Down

0 comments on commit a1b28e8

Please sign in to comment.