-
Notifications
You must be signed in to change notification settings - Fork 70
Preserve empty JSON schema object maps #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,28 @@ public function getParameters(): ?array | |
| return $this->parameters; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the function parameters schema in a JSON-serializable form. | ||
| * | ||
| * JSON Schema object-map fields such as properties must encode as JSON | ||
| * objects even when empty. PHP arrays cannot preserve that distinction | ||
| * without casting the empty map before serialization. | ||
| * | ||
| * @since 0.1.0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| * | ||
| * @return array<string, mixed>|\stdClass|null The JSON-serializable parameters schema. | ||
| */ | ||
| public function getJsonSerializableParameters() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above, this really shouldn't be a public method. it's oddly specific to be part of a public API, and doesn't seem needed as public anyway. |
||
| { | ||
| if ($this->parameters === null) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var array<string, mixed>|\stdClass $parameters */ | ||
| $parameters = $this->prepareJsonSchemaObjectMaps($this->parameters, self::KEY_PARAMETERS); | ||
| return $parameters; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
|
|
@@ -143,6 +165,69 @@ public function toArray(): array | |
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since 0.1.0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above. also applies throughout the PR. |
||
| */ | ||
| #[\ReturnTypeWillChange] | ||
| public function jsonSerialize() | ||
| { | ||
| $data = $this->toArray(); | ||
|
|
||
| if ($this->parameters !== null) { | ||
| $data[self::KEY_PARAMETERS] = $this->getJsonSerializableParameters(); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * Recursively prepares JSON Schema object-map fields for JSON serialization. | ||
| * | ||
| * @since 0.1.0 | ||
| * | ||
| * @param mixed $value The value to prepare. | ||
| * @param string|null $key The current JSON Schema key, if available. | ||
| * @return mixed The prepared value. | ||
| */ | ||
| private function prepareJsonSchemaObjectMaps($value, ?string $key = null) | ||
| { | ||
| if (!is_array($value)) { | ||
| return $value; | ||
| } | ||
|
|
||
| if ($value === [] && $this->isJsonSchemaObjectMapKey($key)) { | ||
| return new \stdClass(); | ||
| } | ||
|
|
||
| foreach ($value as $childKey => $childValue) { | ||
| $value[$childKey] = $this->prepareJsonSchemaObjectMaps( | ||
| $childValue, | ||
| is_string($childKey) ? $childKey : null | ||
| ); | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the given JSON Schema key represents an object map. | ||
| * | ||
| * @since 0.1.0 | ||
| * | ||
| * @param string|null $key The JSON Schema key. | ||
| * @return bool True if the key represents an object map, false otherwise. | ||
| */ | ||
| private function isJsonSchemaObjectMapKey(?string $key): bool | ||
| { | ||
| return in_array( | ||
| $key, | ||
| [self::KEY_PARAMETERS, 'properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'], | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't all this code just the same as this simpler version?