From 256cfb1a8f9357879653a741d806482ec1e6fe3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 19 Jun 2022 07:59:24 +0000 Subject: [PATCH 1/4] Add oneOf support --- app/views/docs/models.phtml | 50 +++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/app/views/docs/models.phtml b/app/views/docs/models.phtml index 21a2df5b0..4f5777bff 100644 --- a/app/views/docs/models.phtml +++ b/app/views/docs/models.phtml @@ -19,17 +19,30 @@ $example = function ($model, $models) use (&$example) $value = $property['x-example'] ?? null; $type = $property['type'] ?? null; $child = str_replace('#/definitions/', '', ($property['items']['$ref'] ?? '')); - $isAnyOf = $property['type'] === 'array' && isset($property['items']['x-anyOf']); + + // We consider anyOf and oneOf the same in docs views + $isXOf = false; + $xIfKey = null; + + if(isset($rule['items']['x-anyOf'])) { + $isXOf = true; + $xIfKey = 'x-anyOf'; + } + + if(isset($rule['items']['x-oneOf'])) { + $isXOf = true; + $xIfKey = 'x-oneOf'; + } if(!empty($child) && \array_key_exists($child, $models)) { $output[$key] = $example($models[$child], $models); } - else if($isAnyOf) { + else if($isXOf) { $output[$key] = \array_map(function($anyOfOption) use($example, $models) { $anyofChild = str_replace('#/definitions/', '', ($anyOfOption['$ref'] ?? '')); return $example($models[$anyofChild], $models); - }, $property['items']['x-anyOf']); + }, $property['items'][$xIfKey]); } else { $output[$key] = $value; @@ -63,7 +76,19 @@ $example = function ($model, $models) use (&$example) $arrayType = (isset($rule['items']['type'])) ? $rule['items']['type'] : ''; $ref = (isset($rule['items']['$ref'])) ? str_replace('#/definitions/', '', $rule['items']['$ref']) : ''; - $isAnyOf = $rule['type'] === 'array' && isset($rule['items']['x-anyOf']); + // We consider anyOf and oneOf the same in docs views + $isXOf = false; + $xIfKey = null; + + if(isset($rule['items']['x-anyOf'])) { + $isXOf = true; + $xIfKey = 'x-anyOf'; + } + + if(isset($rule['items']['x-oneOf'])) { + $isXOf = true; + $xIfKey = 'x-oneOf'; + } ?> @@ -74,13 +99,18 @@ $example = function ($model, $models) use (&$example) escape($ref); ?>[] - - array of
+ + array of
'; + } + ?> + ', \array_map(function($anyOfRule) { - $anyOfRule = (isset($anyOfRule['$ref'])) ? str_replace('#/definitions/', '', $anyOfRule['$ref']) : ''; - return '' . $this->escape($anyOfRule) . ''; - }, $rule['items']['x-anyOf'])); + echo \implode('
', \array_map(function($xOfRule) { + $xOfRule = (isset($xOfRule['$ref'])) ? str_replace('#/definitions/', '', $xOfRule['$ref']) : ''; + return '' . $this->escape($xOfRule) . ''; + }, $rule['items'][$xIfKey])); ?> From 292ebdcdf34350804d5945c5aa68023956764ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 19 Jun 2022 13:16:06 +0000 Subject: [PATCH 2/4] Bug fix --- app/views/docs/models.phtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/docs/models.phtml b/app/views/docs/models.phtml index 4f5777bff..37d9f15d4 100644 --- a/app/views/docs/models.phtml +++ b/app/views/docs/models.phtml @@ -24,12 +24,12 @@ $example = function ($model, $models) use (&$example) $isXOf = false; $xIfKey = null; - if(isset($rule['items']['x-anyOf'])) { + if(isset($property['items']['x-anyOf'])) { $isXOf = true; $xIfKey = 'x-anyOf'; } - if(isset($rule['items']['x-oneOf'])) { + if(isset($property['items']['x-oneOf'])) { $isXOf = true; $xIfKey = 'x-oneOf'; } From 0b5d6d33f503dfe0ab7f71e23105b2b6a5dfcc2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 20 Jun 2022 08:52:29 +0000 Subject: [PATCH 3/4] Bug fix --- app/views/docs/models.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/docs/models.phtml b/app/views/docs/models.phtml index 37d9f15d4..f0cbe7e65 100644 --- a/app/views/docs/models.phtml +++ b/app/views/docs/models.phtml @@ -11,7 +11,7 @@ $example = function ($model, $models) use (&$example) $output = []; $properties = $model['properties'] ?? []; - if(empty($properties) && $model['additionalProperties']) { + if(empty($properties)) { return new \stdClass(); } From 547ede8277f0f75611fa5b03b88ca36c53881493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 20 Jun 2022 10:39:31 +0000 Subject: [PATCH 4/4] Fix non-array json previews --- app/views/docs/models.phtml | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/app/views/docs/models.phtml b/app/views/docs/models.phtml index f0cbe7e65..d8d62105c 100644 --- a/app/views/docs/models.phtml +++ b/app/views/docs/models.phtml @@ -17,32 +17,38 @@ $example = function ($model, $models) use (&$example) foreach($properties as $key => $property) { $value = $property['x-example'] ?? null; + $isArray = $property['array'] ?? false; $type = $property['type'] ?? null; $child = str_replace('#/definitions/', '', ($property['items']['$ref'] ?? '')); // We consider anyOf and oneOf the same in docs views $isXOf = false; - $xIfKey = null; + $xOfKey = null; if(isset($property['items']['x-anyOf'])) { $isXOf = true; - $xIfKey = 'x-anyOf'; + $xOfKey = 'x-anyOf'; } if(isset($property['items']['x-oneOf'])) { $isXOf = true; - $xIfKey = 'x-oneOf'; + $xOfKey = 'x-oneOf'; } - if(!empty($child) && \array_key_exists($child, $models)) { $output[$key] = $example($models[$child], $models); } else if($isXOf) { - $output[$key] = \array_map(function($anyOfOption) use($example, $models) { - $anyofChild = str_replace('#/definitions/', '', ($anyOfOption['$ref'] ?? '')); - return $example($models[$anyofChild], $models); - }, $property['items'][$xIfKey]); + if($isArray) { + $output[$key] = \array_map(function($anyOfOption) use($example, $models) { + $anyofChild = str_replace('#/definitions/', '', ($anyOfOption['$ref'] ?? '')); + return $example($models[$anyofChild], $models); + }, $property['items'][$xOfKey]); + } else { + $model = $property['items'][$xOfKey][0]; + $anyofChild = str_replace('#/definitions/', '', ($model['$ref'] ?? '')); + $output[$key] = $example($models[$anyofChild], $models); + } } else { $output[$key] = $value; @@ -78,16 +84,16 @@ $example = function ($model, $models) use (&$example) // We consider anyOf and oneOf the same in docs views $isXOf = false; - $xIfKey = null; + $xOfKey = null; if(isset($rule['items']['x-anyOf'])) { $isXOf = true; - $xIfKey = 'x-anyOf'; + $xOfKey = 'x-anyOf'; } if(isset($rule['items']['x-oneOf'])) { $isXOf = true; - $xIfKey = 'x-oneOf'; + $xOfKey = 'x-oneOf'; } ?> @@ -110,7 +116,7 @@ $example = function ($model, $models) use (&$example) echo \implode('
', \array_map(function($xOfRule) { $xOfRule = (isset($xOfRule['$ref'])) ? str_replace('#/definitions/', '', $xOfRule['$ref']) : ''; return '' . $this->escape($xOfRule) . ''; - }, $rule['items'][$xIfKey])); + }, $rule['items'][$xOfKey])); ?>