Repository navigation
fix(policies): allow mixed properties in policy value contracts #9453
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
Open
lfals
wants to merge
3
commits into
LibreSign:main
Choose a base branch
from
lfals:fix/policy-value-openapi-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| --- a/src/OpenApiType.php | ||
| +++ b/src/OpenApiType.php | ||
| @@ -79,6 +79,87 @@ | ||
|
|
||
| // https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-354037150 | ||
| return $type !== 'object' && $anyOf === null && ($allOf === null || (($nullable ?? false) && count($allOf) === 1)); | ||
| + } | ||
| + | ||
| + /** | ||
| + * OpenAPI 3.0 ignores keywords beside a Reference Object. Description and | ||
| + * a default that conforms to the schema have to live on a Schema Object | ||
| + * that wraps the reference. | ||
| + * | ||
| + * @param array<string, array<string, mixed>> $schemas | ||
| + */ | ||
| + private function hasSchemaObjectKeywords(bool $isParameter, array $schemas): bool { | ||
| + if ($this->type !== null || $this->format !== null || $this->nullable || $this->deprecated) { | ||
| + return true; | ||
| + } | ||
| + if ($this->hasDefaultValue && !$this->hasUnknownDefaultValue && !$this->shouldOmitDefault($schemas)) { | ||
| + return true; | ||
| + } | ||
| + if ($this->enum !== null || $this->items !== null || $this->additionalProperties !== null) { | ||
| + return true; | ||
| + } | ||
| + if ($this->description !== null && $this->description !== '' && !$isParameter) { | ||
| + return true; | ||
| + } | ||
| + if ($this->minLength !== null || $this->maxLength !== null || $this->minimum !== null || $this->maximum !== null || $this->minItems !== null || $this->maxItems !== null) { | ||
| + return true; | ||
| + } | ||
| + if ($this->required !== null || ($this->properties !== null && $this->properties !== [])) { | ||
| + return true; | ||
| + } | ||
| + if ($this->oneOf !== null || $this->anyOf !== null || $this->allOf !== null) { | ||
| + return true; | ||
| + } | ||
| + | ||
| + return false; | ||
| + } | ||
| + | ||
| + /** | ||
| + * An empty PHP array is the extractor's stand-in for an unspecified object. | ||
| + * Beside a $ref it used to be ignored. Promoting it makes the default an | ||
| + * array while the referenced schema is an object. | ||
| + * | ||
| + * @param array<string, array<string, mixed>> $schemas | ||
| + */ | ||
| + private function shouldOmitDefault(array $schemas): bool { | ||
| + if (!is_array($this->defaultValue) || count($this->defaultValue) !== 0) { | ||
| + return false; | ||
| + } | ||
| + if ($this->type === 'object' || $this->type === 'array') { | ||
| + return false; | ||
| + } | ||
| + | ||
| + return $this->referencedSchemaIsObject($schemas); | ||
| + } | ||
| + | ||
| + /** | ||
| + * @param array<string, array<string, mixed>> $schemas | ||
| + */ | ||
| + private function referencedSchemaIsObject(array $schemas): bool { | ||
| + $refs = []; | ||
| + if ($this->ref !== null) { | ||
| + $refs[] = $this->ref; | ||
| + } | ||
| + foreach ($this->allOf ?? [] as $member) { | ||
| + if ($member->type === 'object') { | ||
| + return true; | ||
| + } | ||
| + if ($member->ref !== null) { | ||
| + $refs[] = $member->ref; | ||
| + } | ||
| + } | ||
| + foreach ($refs as $ref) { | ||
| + $prefix = '#/components/schemas/'; | ||
| + if (!str_starts_with($ref, $prefix)) { | ||
| + continue; | ||
| + } | ||
| + $schema = $schemas[substr($ref, strlen($prefix))] ?? null; | ||
| + if (is_array($schema) && ($schema['type'] ?? null) === 'object') { | ||
| + return true; | ||
| + } | ||
| + } | ||
| + | ||
| + return false; | ||
| } | ||
|
|
||
| /** | ||
| @@ -95,6 +176,18 @@ | ||
| ))->toArray($schemas, $isParameter); | ||
| } | ||
|
|
||
| + if ($this->ref !== null && $this->hasSchemaObjectKeywords($isParameter, $schemas)) { | ||
| + $reference = new self( | ||
| + context: $this->context, | ||
| + ref: $this->ref, | ||
| + ); | ||
| + $schema = clone $this; | ||
| + $schema->ref = null; | ||
| + $schema->allOf = [...($schema->allOf ?? []), $reference]; | ||
| + | ||
| + return $schema->toArray($schemas, $isParameter); | ||
| + } | ||
| + | ||
| $values = []; | ||
| if ($this->ref !== null) { | ||
| $values['$ref'] = $this->ref; | ||
| @@ -111,7 +204,7 @@ | ||
| if ($this->deprecated) { | ||
| $values['deprecated'] = true; | ||
| } | ||
| - if ($this->hasDefaultValue && !$this->hasUnknownDefaultValue) { | ||
| + if ($this->hasDefaultValue && !$this->hasUnknownDefaultValue && !$this->shouldOmitDefault($schemas)) { | ||
| $values['default'] = $this->type === 'object' && is_array($this->defaultValue) && count($this->defaultValue) === 0 ? new stdClass() : $this->defaultValue; | ||
| } | ||
| if ($this->enum !== null) { | ||
| @@ -243,6 +336,17 @@ | ||
| $key = $node->genericTypes[0] instanceof IdentifierTypeNode ? $node->genericTypes[0]->name : $node->genericTypes[0]->type->name; | ||
| $allowedTypes = ['string', 'lowercase-string', 'non-empty-string', 'non-empty-lowercase-string', 'class-string']; | ||
| if (in_array($key, $allowedTypes, true)) { | ||
| + $valueType = $node->genericTypes[1]; | ||
| + // array<string, mixed> is an object whose values are unrestricted. | ||
| + // A nested `{type: object}` schema would reject scalars, lists and null. | ||
| + if ($valueType instanceof IdentifierTypeNode && $valueType->name === 'mixed') { | ||
| + return new OpenApiType( | ||
| + context: $context, | ||
| + type: 'object', | ||
| + additionalProperties: true, | ||
| + ); | ||
| + } | ||
| + | ||
| return new OpenApiType( | ||
| context: $context, | ||
| type: 'object', | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The generalized
$refwrapper also affects schemas outside the policy endpoints.In the regenerated
openapi-full.json, properties such asfile,settings, andcoordinatesnow exposedefault: []alongsideallOfreferences to schemas declared astype: object. Previously, these defaults were ignored as$refsiblings.This introduces an inconsistency between the effective schema and its declared default, potentially affecting generated clients.
Could we verify this with a focused schema-conformance and client-generation test? If the mismatch is confirmed, please correct it at the source/generator boundary and regenerate the contracts. The policy-value fix itself should be preserved.