Skip to content

Commit

Permalink
Merge pull request #84 from cnizzardini/bug/issue-82-xml-root-element
Browse files Browse the repository at this point in the history
XML example cannot be generated; root element name is undefined (#82)
  • Loading branch information
cnizzardini authored Jun 28, 2020
2 parents 1262264 + 01aee9a commit 0832efe
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 55 deletions.
4 changes: 4 additions & 0 deletions assets/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ components:
message:
type: string
example: Internal Error
xml:
name: response
OperationResult:
type: object
properties:
result:
type: boolean
xml:
name: response
33 changes: 27 additions & 6 deletions src/Lib/OpenApi/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Schema implements JsonSerializable
/** @var string|null */
private $title;

/** @var string|null */
private $description;
/** @var string */
private $description = '';

/** @var string */
private $type = '';
Expand Down Expand Up @@ -50,6 +50,9 @@ class Schema implements JsonSerializable
/** @var string */
private $format;

/** @var Xml|null */
private $xml;

/**
* @return array
*/
Expand All @@ -66,8 +69,8 @@ public function toArray() : array
}

// remove empty properties to avoid swagger.json clutter
foreach (['title','description','properties','items','oneOf','anyOf','allOf','not','enum','format','type'] as $v) {
if (empty($vars[$v]) || is_null($vars[$v])) {
foreach (['title','properties','items','oneOf','anyOf','allOf','not','enum','format','type', 'xml'] as $v) {
if (array_key_exists($v, $vars) && (empty($vars[$v]) || is_null($vars[$v]))) {
unset($vars[$v]);
}
}
Expand Down Expand Up @@ -213,10 +216,10 @@ public function getDescription(): ?string
}

/**
* @param string|null $description
* @param string $description
* @return Schema
*/
public function setDescription(?string $description): Schema
public function setDescription(string $description): Schema
{
$this->description = $description;
return $this;
Expand Down Expand Up @@ -347,4 +350,22 @@ public function setFormat(string $format): Schema
$this->format = $format;
return $this;
}

/**
* @return Xml|null
*/
public function getXml(): ?Xml
{
return $this->xml;
}

/**
* @param Xml|null $xml
* @return Schema
*/
public function setXml(?Xml $xml): Schema
{
$this->xml = $xml;
return $this;
}
}
149 changes: 149 additions & 0 deletions src/Lib/OpenApi/Xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace SwaggerBake\Lib\OpenApi;

use JsonSerializable;

/**
* Class Xml
* @package SwaggerBake\Lib\OpenApi
*/
class Xml implements JsonSerializable
{
/** @var string */
private $name;

/** @var string|null */
private $namespace;

/** @var string|null */
private $prefix;

/** @var bool|null */
private $attribute;

/** @var bool|null */
private $wrapped;

/**
* @return array
*/
public function toArray() : array
{
$vars = get_object_vars($this);

// remove properties if they are set to their defaults (to avoid json clutter)
foreach (['attribute','wrapped'] as $v) {
if (array_key_exists($v, $vars) && $vars[$v] === false) {
unset($vars[$v]);
}
}

// remove empty properties to avoid swagger.json clutter
foreach (['namespace','prefix','attribute','wrapped'] as $v) {
if (array_key_exists($v, $vars) && (is_null($vars[$v]) || empty($vars[$v]))) {
unset($vars[$v]);
}
}

return $vars;
}

/**
* @return array|mixed
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
* @return Xml
*/
public function setName(string $name): Xml
{
$this->name = $name;
return $this;
}

/**
* @return string|null
*/
public function getNamespace(): ?string
{
return $this->namespace;
}

/**
* @param string|null $namespace
* @return Xml
*/
public function setNamespace(?string $namespace): Xml
{
$this->namespace = $namespace;
return $this;
}

/**
* @return string|null
*/
public function getPrefix(): ?string
{
return $this->prefix;
}

/**
* @param string|null $prefix
* @return Xml
*/
public function setPrefix(?string $prefix): Xml
{
$this->prefix = $prefix;
return $this;
}

/**
* @return bool|null
*/
public function getAttribute(): ?bool
{
return $this->attribute;
}

/**
* @param bool|null $attribute
* @return Xml
*/
public function setAttribute(?bool $attribute): Xml
{
$this->attribute = $attribute;
return $this;
}

/**
* @return bool|null
*/
public function getWrapped(): ?bool
{
return $this->wrapped;
}

/**
* @param bool|null $wrapped
* @return Xml
*/
public function setWrapped(?bool $wrapped): Xml
{
$this->wrapped = $wrapped;
return $this;
}
}
7 changes: 7 additions & 0 deletions src/Lib/Operation/OperationRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SwaggerBake\Lib\OpenApi\RequestBody;
use SwaggerBake\Lib\OpenApi\Schema;
use SwaggerBake\Lib\OpenApi\SchemaProperty;
use SwaggerBake\Lib\OpenApi\Xml;
use SwaggerBake\Lib\Utility\DocBlockUtility;

/**
Expand Down Expand Up @@ -251,6 +252,12 @@ private function assignSchema() : void
$schema->pushProperty($schemaProperty);
}

if ($mimeType == 'application/xml') {
$schema->setXml(
(new Xml())->setName(strtolower($this->schema->getName()))
);
}

$requestBody->pushContent(
(new Content())
->setMimeType($mimeType)
Expand Down
66 changes: 41 additions & 25 deletions src/Lib/Operation/OperationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SwaggerBake\Lib\OpenApi\Operation;
use SwaggerBake\Lib\OpenApi\Response;
use SwaggerBake\Lib\OpenApi\Schema;
use SwaggerBake\Lib\OpenApi\Xml;

/**
* Class OperationResponse
Expand Down Expand Up @@ -116,22 +117,20 @@ private function assignDocBlockExceptions() : void

$throws = $this->doc->getTagsByName('throws');

$mimeTypes = $this->config->getResponseContentTypes();
$mimeType = reset($mimeTypes);

foreach ($throws as $throw) {
$exception = new ExceptionHandler($throw);

$this->operation->pushResponse(
(new Response())
->setCode($exception->getCode())
->setDescription($exception->getMessage())
->pushContent(
(new Content())
->setMimeType($mimeType)
->setSchema('#/components/schemas/' . $this->config->getExceptionSchema())
)
);
$response = (new Response())->setCode($exception->getCode())->setDescription($exception->getMessage());

foreach ($this->config->getResponseContentTypes() as $mimeType) {
$response->pushContent(
(new Content())
->setMimeType($mimeType)
->setSchema('#/components/schemas/' . $this->config->getExceptionSchema())
);
}

$this->operation->pushResponse($response);
}
}

Expand All @@ -153,13 +152,20 @@ private function assignSchema() : void
return;
}

$schema = clone $this->schema;

if (in_array(strtolower($this->route->getAction()),['index'])) {
$response = (new Response())->setCode('200');

foreach ($this->config->getResponseContentTypes() as $mimeType) {

if ($mimeType == 'application/xml') {
$schema->setXml((new Xml())->setName('response'));
}

$response->pushContent(
(new Content())
->setSchema($this->schema)
->setSchema($schema)
->setMimeType($mimeType)
);
}
Expand All @@ -171,9 +177,14 @@ private function assignSchema() : void
$response = (new Response())->setCode('200');

foreach ($this->config->getResponseContentTypes() as $mimeType) {

if ($mimeType == 'application/xml') {
$schema->setXml((new Xml())->setName('response'));
}

$response->pushContent(
(new Content())
->setSchema($this->schema)
->setSchema($schema)
->setMimeType($mimeType)
);
}
Expand Down Expand Up @@ -205,17 +216,22 @@ private function assignDefaultResponses() : void
return;
}

$types = $this->config->getResponseContentTypes();
$response = (new Response())->setCode('200');

$this->operation->pushResponse(
(new Response())
->setCode('200')
->pushContent(
(new Content())
->setMimeType(reset($types))
->setSchema(new Schema())
)
);
foreach ($this->config->getResponseContentTypes() as $mimeType) {

$schema = (new Schema())->setDescription('');

if ($mimeType == 'application/xml') {
$schema->setXml((new Xml())->setName('response'));
}

$response->pushContent(
(new Content())->setMimeType($mimeType)->setSchema($schema)
);
}

$this->operation->pushResponse($response);

return;
}
Expand Down
Loading

0 comments on commit 0832efe

Please sign in to comment.