From 21095466af5d53843edd0fe4ec415f11647f3b29 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 9 Dec 2025 15:23:43 +0100 Subject: [PATCH 1/5] EnumPostProcessor.php fix https://github.com/wol-soft/php-json-schema-model-generator/issues/94#issuecomment-3631540381 --- src/SchemaProcessor/PostProcessor/EnumFilter.php | 4 +++- src/SchemaProcessor/PostProcessor/EnumPostProcessor.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/SchemaProcessor/PostProcessor/EnumFilter.php b/src/SchemaProcessor/PostProcessor/EnumFilter.php index be76814..e03c89d 100644 --- a/src/SchemaProcessor/PostProcessor/EnumFilter.php +++ b/src/SchemaProcessor/PostProcessor/EnumFilter.php @@ -9,6 +9,8 @@ class EnumFilter implements TransformingFilterInterface { + public const FILTER_TOKEN_GENERATOR_ENUM = 'php_model_generator_enum'; + public function getAcceptedTypes(): array { return ['string', 'integer', 'null']; @@ -16,7 +18,7 @@ public function getAcceptedTypes(): array public function getToken(): string { - return 'php_model_generator_enum'; + return self::FILTER_TOKEN_GENERATOR_ENUM; } public function getFilter(): array diff --git a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php index 231c6a4..eca3fd8 100644 --- a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php +++ b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php @@ -78,6 +78,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu $enumSignature = ArrayHash::hash($json, ['enum', 'enum-map', 'title', '$id']); $enumName = $json['title'] ?? basename($json['$id'] ?? $schema->getClassName() . ucfirst($property->getName())); + $enumName = preg_replace('#\W#', '_', $enumName); if (!isset($this->generatedEnums[$enumSignature])) { $this->generatedEnums[$enumSignature] = [ @@ -153,6 +154,7 @@ private function checkForExistingTransformingFilter(PropertyInterface $property) if ($validator instanceof FilterValidator && $validator->getFilter() instanceof TransformingFilterInterface + && $validator->getFilter()->getToken() !== EnumFilter::FILTER_TOKEN_GENERATOR_ENUM ) { throw new SchemaException(sprintf( "Can't apply enum filter to an already transformed value on property %s in file %s", From a572d0d8478214dcd4cad144279c9b5051b8cfeb Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 11 Dec 2025 09:52:25 +0100 Subject: [PATCH 2/5] EnumPostProcessor.php fix generator using default value string representation instead of case name of enum see https://github.com/wol-soft/php-json-schema-model-generator/issues/99 --- .../PostProcessor/EnumPostProcessor.php | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php index eca3fd8..1c23c7d 100644 --- a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php +++ b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php @@ -80,6 +80,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu ?? basename($json['$id'] ?? $schema->getClassName() . ucfirst($property->getName())); $enumName = preg_replace('#\W#', '_', $enumName); + $jsonEnumMap = $json['enum-map'] ?? null; if (!isset($this->generatedEnums[$enumSignature])) { $this->generatedEnums[$enumSignature] = [ 'name' => $enumName, @@ -88,7 +89,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu $schema->getJsonSchema(), $enumName, $values, - $json['enum-map'] ?? null, + $jsonEnumMap, ), ]; } else { @@ -116,7 +117,8 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu $property->setType($inputType, new PropertyType($name, !$property->isRequired()), true); if ($property->getDefaultValue() && in_array($property->getJsonSchema()->getJson()['default'], $values)) { - $property->setDefaultValue("$name::{$property->getJsonSchema()->getJson()['default']}", true); + $defaultValueEnumCaseName = $this->getCaseName($property->getJsonSchema()->getJson()['default'], $schema->getJsonSchema()); + $property->setDefaultValue("$name::{$defaultValueEnumCaseName}", true); } // remove the enum validator as the validation is performed by the PHP enum @@ -236,17 +238,7 @@ private function renderEnum( array $values, ?array $map, ): string { - $cases = []; - - foreach ($values as $value) { - $caseName = ucfirst(NormalizedName::from($map ? array_search($value, $map, true) : $value, $jsonSchema)); - - if (preg_match('/^\d/', $caseName) === 1) { - $caseName = "_$caseName"; - } - - $cases[$caseName] = var_export($value, true); - } + $cases = $this->valuesToCases($values, $map, $jsonSchema); $backedType = null; switch ($this->getArrayTypes($values)) { @@ -290,4 +282,27 @@ private function renderEnum( return $fqcn; } + + private function valuesToCases(array $values, ?array $map, JsonSchema $jsonSchema): array + { + $cases = []; + + foreach ($values as $value) { + $name = $map ? array_search($value, $map, true) : $value; + $caseName = $this->getCaseName($name, $jsonSchema); + + $cases[$caseName] = var_export($value, true); + } + return $cases; + } + + private function getCaseName(string $name, JsonSchema $jsonSchema): string + { + $caseName = ucfirst(NormalizedName::from($name, $jsonSchema)); + + if (preg_match('/^\d/', $caseName) === 1) { + $caseName = "_$caseName"; + } + return $caseName; + } } From 4063634959ab867f7495cae73342e15f943ad451 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 9 Dec 2025 15:23:43 +0100 Subject: [PATCH 3/5] EnumPostProcessor.php fix https://github.com/wol-soft/php-json-schema-model-generator/issues/94#issuecomment-3631540381 --- src/SchemaProcessor/PostProcessor/EnumFilter.php | 4 +++- src/SchemaProcessor/PostProcessor/EnumPostProcessor.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/SchemaProcessor/PostProcessor/EnumFilter.php b/src/SchemaProcessor/PostProcessor/EnumFilter.php index be76814..e03c89d 100644 --- a/src/SchemaProcessor/PostProcessor/EnumFilter.php +++ b/src/SchemaProcessor/PostProcessor/EnumFilter.php @@ -9,6 +9,8 @@ class EnumFilter implements TransformingFilterInterface { + public const FILTER_TOKEN_GENERATOR_ENUM = 'php_model_generator_enum'; + public function getAcceptedTypes(): array { return ['string', 'integer', 'null']; @@ -16,7 +18,7 @@ public function getAcceptedTypes(): array public function getToken(): string { - return 'php_model_generator_enum'; + return self::FILTER_TOKEN_GENERATOR_ENUM; } public function getFilter(): array diff --git a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php index 231c6a4..eca3fd8 100644 --- a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php +++ b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php @@ -78,6 +78,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu $enumSignature = ArrayHash::hash($json, ['enum', 'enum-map', 'title', '$id']); $enumName = $json['title'] ?? basename($json['$id'] ?? $schema->getClassName() . ucfirst($property->getName())); + $enumName = preg_replace('#\W#', '_', $enumName); if (!isset($this->generatedEnums[$enumSignature])) { $this->generatedEnums[$enumSignature] = [ @@ -153,6 +154,7 @@ private function checkForExistingTransformingFilter(PropertyInterface $property) if ($validator instanceof FilterValidator && $validator->getFilter() instanceof TransformingFilterInterface + && $validator->getFilter()->getToken() !== EnumFilter::FILTER_TOKEN_GENERATOR_ENUM ) { throw new SchemaException(sprintf( "Can't apply enum filter to an already transformed value on property %s in file %s", From f75c5456f8ac7db6d0778c447cd1a869ea3d220d Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 11 Dec 2025 09:52:25 +0100 Subject: [PATCH 4/5] EnumPostProcessor.php fix generator using default value string representation instead of case name of enum see https://github.com/wol-soft/php-json-schema-model-generator/issues/99 --- .../PostProcessor/EnumPostProcessor.php | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php index eca3fd8..0fc04db 100644 --- a/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php +++ b/src/SchemaProcessor/PostProcessor/EnumPostProcessor.php @@ -116,7 +116,8 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu $property->setType($inputType, new PropertyType($name, !$property->isRequired()), true); if ($property->getDefaultValue() && in_array($property->getJsonSchema()->getJson()['default'], $values)) { - $property->setDefaultValue("$name::{$property->getJsonSchema()->getJson()['default']}", true); + $defaultValueEnumCaseName = $this->getCaseName($property->getJsonSchema()->getJson()['default'], $schema->getJsonSchema()); + $property->setDefaultValue("$name::{$defaultValueEnumCaseName}", true); } // remove the enum validator as the validation is performed by the PHP enum @@ -236,17 +237,7 @@ private function renderEnum( array $values, ?array $map, ): string { - $cases = []; - - foreach ($values as $value) { - $caseName = ucfirst(NormalizedName::from($map ? array_search($value, $map, true) : $value, $jsonSchema)); - - if (preg_match('/^\d/', $caseName) === 1) { - $caseName = "_$caseName"; - } - - $cases[$caseName] = var_export($value, true); - } + $cases = $this->valuesToCases($values, $map, $jsonSchema); $backedType = null; switch ($this->getArrayTypes($values)) { @@ -290,4 +281,27 @@ private function renderEnum( return $fqcn; } + + private function valuesToCases(array $values, ?array $map, JsonSchema $jsonSchema): array + { + $cases = []; + + foreach ($values as $value) { + $name = $map ? array_search($value, $map, true) : $value; + $caseName = $this->getCaseName($name, $jsonSchema); + + $cases[$caseName] = var_export($value, true); + } + return $cases; + } + + private function getCaseName(string $name, JsonSchema $jsonSchema): string + { + $caseName = ucfirst(NormalizedName::from($name, $jsonSchema)); + + if (preg_match('/^\d/', $caseName) === 1) { + $caseName = "_$caseName"; + } + return $caseName; + } } From 5abb0061479597c16bcf2ecc60c35ccdbdb58dd4 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 11 Dec 2025 16:28:10 +0100 Subject: [PATCH 5/5] ComposedItem.phptpl fix mergedProperty array_merge leading to runtime error as originalModelData is not array in my use case (and its not a mergedProperty either) --- src/Templates/Validator/ComposedItem.phptpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Templates/Validator/ComposedItem.phptpl b/src/Templates/Validator/ComposedItem.phptpl index 56a103e..ca99d88 100644 --- a/src/Templates/Validator/ComposedItem.phptpl +++ b/src/Templates/Validator/ComposedItem.phptpl @@ -88,9 +88,11 @@ $proposedValue = $proposedValue ?? $value; {% endif %} + {% if mergedProperty %} if (is_object($value)) { $modifiedValues = array_merge($modifiedValues, $this->{{ modifiedValuesMethod }}($originalModelData, $value)); } + {% endif %} {% if viewHelper.isMutableBaseValidator(generatorConfiguration, isBaseValidator) %} {% if not generatorConfiguration.collectErrors() %} if (isset($validatorIndex)) {