Skip to content

Commit eff48d9

Browse files
author
Enno Woortmann
committed
Extend Schema exceptions to contain the file causing the error
1 parent d88f8b9 commit eff48d9

16 files changed

+105
-29
lines changed

src/Model/Property/Property.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ class Property implements PropertyInterface
6060
*/
6161
public function __construct(string $name, string $type, JsonSchema $jsonSchema, string $description = '')
6262
{
63-
$this->attribute = $this->processAttributeName($name);
6463
$this->name = $name;
6564
$this->type = $type;
6665
$this->jsonSchema = $jsonSchema;
6766
$this->description = $description;
67+
68+
$this->attribute = $this->processAttributeName($name);
6869
}
6970

7071
/**
@@ -260,7 +261,13 @@ function ($element) {
260261
$attributeName = lcfirst(join('', $elements));
261262

262263
if (empty($attributeName)) {
263-
throw new SchemaException("Property name '$name' results in an empty attribute name");
264+
throw new SchemaException(
265+
sprintf(
266+
"Property name '%s' results in an empty attribute name in file %s",
267+
$name,
268+
$this->jsonSchema->getFile()
269+
)
270+
);
264271
}
265272

266273
return $attributeName;

src/Model/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class Schema
5353
*
5454
* @param string $classPath
5555
* @param string $className
56-
* @param JsonSchema|null $schema
56+
* @param JsonSchema $schema
5757
* @param SchemaDefinitionDictionary|null $dictionary
5858
*/
5959
public function __construct(
6060
string $classPath,
6161
string $className,
62-
JsonSchema $schema = null,
62+
JsonSchema $schema,
6363
SchemaDefinitionDictionary $dictionary = null
6464
) {
6565
$this->className = $className;

src/Model/SchemaDefinition/SchemaDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function resolveReference(
7878

7979
while ($segment = array_shift($path)) {
8080
if (!isset($jsonSchema[$segment])) {
81-
throw new SchemaException("Unresolved path segment: $segment");
81+
throw new SchemaException("Unresolved path segment $segment in file {$this->source->getFile()}");
8282
}
8383

8484
$jsonSchema = $jsonSchema[$segment];

src/Model/Validator/FilterValidator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ private function validateFilterCompatibilityWithBaseType(FilterInterface $filter
140140
) {
141141
throw new SchemaException(
142142
sprintf(
143-
'Filter %s is not compatible with property type %s for property %s',
143+
'Filter %s is not compatible with property type %s for property %s in file %s',
144144
$filter->getToken(),
145145
$property->getType(),
146-
$property->getName()
146+
$property->getName(),
147+
$property->getJsonSchema()->getFile()
147148
)
148149
);
149150
}
@@ -174,10 +175,11 @@ private function validateFilterCompatibilityWithTransformedType(
174175
) {
175176
throw new SchemaException(
176177
sprintf(
177-
'Filter %s is not compatible with transformed property type %s for property %s',
178+
'Filter %s is not compatible with transformed property type %s for property %s in file %s',
178179
$filter->getToken(),
179180
$transformedType->getName(),
180-
$property->getName()
181+
$property->getName(),
182+
$property->getJsonSchema()->getFile()
181183
)
182184
);
183185
}

src/PropertyProcessor/ComposedValue/IfProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ protected function generateValidators(PropertyInterface $property, JsonSchema $p
3333
$json = $propertySchema->getJson()['propertySchema']->getJson();
3434

3535
if (!isset($json['then']) && !isset($json['else'])) {
36-
throw new SchemaException('Incomplete conditional composition');
36+
throw new SchemaException(
37+
sprintf(
38+
'Incomplete conditional composition for property %s in file %s',
39+
$property->getName(),
40+
$property->getJsonSchema()->getFile()
41+
)
42+
);
3743
}
3844

3945
$propertyFactory = new PropertyFactory(new PropertyProcessorFactory());

src/PropertyProcessor/Filter/FilterProcessor.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ public function process(
5959
}
6060

6161
if (!($filter = $generatorConfiguration->getFilter($filterToken))) {
62-
throw new SchemaException("Unsupported filter $filterToken");
62+
throw new SchemaException(
63+
sprintf(
64+
'Unsupported filter %s on property %s in file %s',
65+
$filterToken,
66+
$property->getName(),
67+
$property->getJsonSchema()->getFile()
68+
)
69+
);
6370
}
6471

6572
$property->addValidator(
@@ -70,12 +77,20 @@ public function process(
7077
if ($filter instanceof TransformingFilterInterface) {
7178
if ($property->getType() === 'array') {
7279
throw new SchemaException(
73-
"Applying a transforming filter to the array property {$property->getName()} is not supported"
80+
sprintf(
81+
'Applying a transforming filter to the array property %s is not supported in file %s',
82+
$property->getName(),
83+
$property->getJsonSchema()->getFile()
84+
)
7485
);
7586
}
7687
if ($transformingFilter) {
7788
throw new SchemaException(
78-
"Applying multiple transforming filters for property {$property->getName()} is not supported"
89+
sprintf(
90+
'Applying multiple transforming filters for property %s is not supported in file %s',
91+
$property->getName(),
92+
$property->getJsonSchema()->getFile()
93+
)
7994
);
8095
}
8196

src/PropertyProcessor/Property/AbstractTypedValueProcessor.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,34 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
4949
$property = parent::process($propertyName, $propertySchema);
5050

5151
if (isset($propertySchema->getJson()['default'])) {
52-
$this->setDefaultValue($property, $propertySchema->getJson()['default']);
52+
$this->setDefaultValue($property, $propertySchema->getJson()['default'], $propertySchema);
5353
}
5454

5555
return $property;
5656
}
5757

5858
/**
5959
* @param PropertyInterface $property
60-
* @param $default
60+
* @param mixed $default
61+
* @param JsonSchema $propertySchema
6162
*
6263
* @throws SchemaException
6364
*/
64-
public function setDefaultValue(PropertyInterface $property, $default): void
65+
public function setDefaultValue(PropertyInterface $property, $default, JsonSchema $propertySchema): void
6566
{
6667
// allow integer default values for Number properties
6768
if ($this instanceof NumberProcessor && is_int($default)) {
6869
$default = (float) $default;
6970
}
7071

7172
if (!$this->getTypeCheckFunction()($default)) {
72-
throw new SchemaException("Invalid type for default value of property {$property->getName()}");
73+
throw new SchemaException(
74+
sprintf(
75+
"Invalid type for default value of property %s in file %s",
76+
$property->getName(),
77+
$propertySchema->getFile()
78+
)
79+
);
7380
}
7481

7582
$property->setDefaultValue($default);

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,13 @@ protected function transferComposedPropertiesToSchema(PropertyInterface $propert
254254

255255
foreach ($validator->getComposedProperties() as $composedProperty) {
256256
if (!$composedProperty->getNestedSchema()) {
257-
throw new SchemaException('No nested schema for composed property found');
257+
throw new SchemaException(
258+
sprintf(
259+
"No nested schema for composed property %s in file %s found",
260+
$property->getName(),
261+
$property->getJsonSchema()->getFile()
262+
)
263+
);
258264
}
259265

260266
foreach ($composedProperty->getNestedSchema()->getProperties() as $property) {

src/PropertyProcessor/Property/BasereferenceProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
3131

3232
if (!$property->getNestedSchema()) {
3333
throw new SchemaException(
34-
"A referenced schema on base level must provide an object definition [$propertyName]"
34+
sprintf(
35+
'A referenced schema on base level must provide an object definition for property %s in file %s',
36+
$propertyName,
37+
$propertySchema->getFile()
38+
)
3539
);
3640
}
3741

src/PropertyProcessor/Property/MultiTypeProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function processSubProperties(
169169

170170
if ($defaultValue !== null && $propertyProcessor instanceof AbstractTypedValueProcessor) {
171171
try {
172-
$propertyProcessor->setDefaultValue($property, $defaultValue);
172+
$propertyProcessor->setDefaultValue($property, $defaultValue, $propertySchema);
173173
} catch (SchemaException $e) {
174174
$invalidDefaultValues++;
175175
$invalidDefaultValueException = $e;

0 commit comments

Comments
 (0)