Skip to content

Commit

Permalink
Support for readonly and writeonly media types
Browse files Browse the repository at this point in the history
  • Loading branch information
christopheg committed Mar 18, 2022
1 parent 4d87927 commit d2e3b0e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,30 @@ The trait will implement the following methods that can be overridden:

public function get_openapi_media_type(): \Skeleton\Application\Api\Media\Type;

Returns the full Media\Type object for the object. By default it returns
a media type with type 'object' and all properties returned by
Returns the full Media\Type object for the object. By default it returns
a media type with type 'object' and all properties returned by
get_openapi_component_properties

public function get_openapi_component_name(): string;

Returns a friendly name for the component. By default the name is extracted
from the component classname. The namespace from the app is ignored.

public function get_openapi_component_properties(): array;

Returns an array with Media\Type objects. The key of each element in the array
becomes the name of the property.

public function get_openapi_additional_properties();

If the component has optional properties, they can be returned in this method.
The array should have the same structure as the one returned by
The array should have the same structure as the one returned by
get_openapi_component_properties: an array of Media\Type objects.

public function get_openapi_description(): string;

A description for the component.

public function get_openapi_example(): array;

An example for the component.
Expand Down Expand Up @@ -298,4 +298,5 @@ Media types can have the following other properties:
description: a description of the media type
required: boolean, indicates if the media type is required
nullable: boolean, can the value be null

readonly: only used in GET
writeonly: only used in POST/PUT
82 changes: 61 additions & 21 deletions lib/Skeleton/Application/Api/Media/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@
class Type {

/**
* The data type
* Additional Properties
*
* @access public
* @var string $type
* @var $mixed $additional_properties
*/
public $type = '';
public $additional_properties = true;

/**
* Description
* An optional description for the media_type
*
* @access public
* @var string $description
*/
public $description = null;

/**
* Example
* An optional example for the media_type
*
* @access public
* @var mixed $example
*/
public $example = null;

/**
* Format
Expand All @@ -35,31 +53,28 @@ class Type {
public $nullable = false;

/**
* Valuetype
* The object in the array or the classname in case of type=object
* Properties
*
* @access public
* @var string $value_type
* @var array $propertiess
*/
public $value_type = null;
public $properties = [];

/**
* Description
* An optional description for the media_type
* Readonly
*
* @access public
* @var string $description
* @var bool $readonly
*/
public $description = null;
public $readonly = null;

/**
* Example
* An optional example for the media_type
* Writeonly
*
* @access public
* @var mixed $example
* @var bool $writeonly
*/
public $example = null;
public $writeonly = null;

/**
* Required
Expand All @@ -70,20 +85,21 @@ class Type {
public $required = false;

/**
* Additional Properties
* The data type
*
* @access public
* @var $mixed $additional_properties
* @var string $type
*/
public $additional_properties = true;
public $type = '';

/**
* Properties
* Valuetype
* The object in the array or the classname in case of type=object
*
* @access public
* @var array $propertiess
* @var string $value_type
*/
public $properties = [];
public $value_type = null;

/**
* Validate
Expand Down Expand Up @@ -264,6 +280,12 @@ private function get_schema_boolean($object_reference = true) {
if (isset($this->nullable) and $this->nullable !== false) {
$schema['nullable'] = $this->nullable;
}
if (isset($this->readonly) and $this->readonly !== false) {
$schema['readOnly'] = $this->readonly;
}
if (isset($this->writeonly) and $this->writeonly !== false) {
$schema['writeOnly'] = $this->writeonly;
}
return $schema;
}

Expand All @@ -288,6 +310,12 @@ private function get_schema_number($object_reference = true) {
if (isset($this->nullable) and $this->nullable !== false) {
$schema['nullable'] = $this->nullable;
}
if (isset($this->readonly) and $this->readonly !== false) {
$schema['readOnly'] = $this->readonly;
}
if (isset($this->writeonly) and $this->writeonly !== false) {
$schema['writeOnly'] = $this->writeonly;
}
return $schema;
}

Expand All @@ -312,6 +340,12 @@ private function get_schema_integer($object_reference = true) {
if (isset($this->nullable) and $this->nullable !== false) {
$schema['nullable'] = $this->nullable;
}
if (isset($this->readonly) and $this->readonly !== false) {
$schema['readOnly'] = $this->readonly;
}
if (isset($this->writeonly) and $this->writeonly !== false) {
$schema['writeOnly'] = $this->writeonly;
}
return $schema;
}

Expand All @@ -336,6 +370,12 @@ private function get_schema_string($object_reference = true) {
if (isset($this->nullable) and $this->nullable !== false) {
$schema['nullable'] = $this->nullable;
}
if (isset($this->readonly) and $this->readonly !== false) {
$schema['readOnly'] = $this->readonly;
}
if (isset($this->writeonly) and $this->writeonly !== false) {
$schema['writeOnly'] = $this->writeonly;
}
return $schema;
}

Expand Down

0 comments on commit d2e3b0e

Please sign in to comment.