Commit f3ecfca3 authored by Brad Jones's avatar Brad Jones
Browse files

Issue #3244983 by bradjones1: Use an anyOf/oneOf compositional schema for allowed values

parent 5dfaa57b
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\jsonapi_schema\Normalizer;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\serialization\Normalizer\NormalizerBase;

@@ -68,7 +69,11 @@ class DataDefinitionNormalizer extends NormalizerBase {
        && !empty($context['parent']->getSetting('allowed_values'))
      ) {
        $allowed_values = $context['parent']->getSetting('allowed_values');
        $property['enum'] = array_keys($allowed_values);
        // Include titles for UI integration.
        // @see https://json-schema.org/understanding-json-schema/reference/generic.html?highlight=enum#annotations
        $composition = $context['cardinality'] === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? 'anyOf' : 'oneOf';
        array_walk($allowed_values, function (&$v, $k) { $v = ['const' => $k, 'title' => $v]; });
        $property[$composition] = array_values($allowed_values);
      }
    }