-
Notifications
You must be signed in to change notification settings - Fork 125
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
[JsonSchema] Use arrayObject for objects #704
base: next
Are you sure you want to change the base?
Conversation
It looks like the tests are also outputting incorrect code. @Korbeil Can you take a look at the fix, if you think it looks good, I can update the tests. Just an aside: I'm currently using the fix in production on the docker API and it's working well. |
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.
I didn't understood you wanted a review on the code before the commend you made today on the issue.
On the fly it seems totally ok to me, also I saw you already generated the docker-php
client with and it seems it does fix your issue so I'm all for it 👍
Thanks @Korbeil, I'll fix the tests this or next week and then you can merge. |
Thanks for this patch. A bit of feedback: This patch fixes the case where Jane emits an empty array instead an empty object, but it introduces issues with other types, ie. nullable objects with local properties like: caseAgent:
type: object
properties:
name:
type: string
description: The name of the agent responsible for the case.
phoneNumber:
type: string
description: A phone number for contacting the agent.
required:
- name
- phoneNumber
nullable: true This changes the normalizer code to: $data['caseAgent'] = new \ArrayObject($this->normalizer->normalize($object->getCaseAgent(), 'json', $context), \ArrayObject::ARRAY_AS_PROPS); and causes a fatal error when the value is This may be the cause of the failing tests, in which case you can ignore me :-) |
Hey, any news about fixing tests ? :) |
Here's what it might look like if we test for null: /**
* @return array|string|int|float|bool|\ArrayObject|null
*/
public function normalize($object, $format = null, array $context = array())
{
$data = array();
if ($object->isInitialized('foo') && null !== $object->getFoo()) {
$data['foo'] = $object->getFoo();
}
if ($object->isInitialized('bar') && null !== $object->getBar()) {
$data['Bar'] = $object->getBar() == null ? null : new \ArrayObject($this->normalizer->normalize($object->getBar(), 'json', $context), \ArrayObject::ARRAY_AS_PROPS);
}
if ($object->isInitialized('baz') && null !== $object->getBaz()) {
$data['Baz'] = $object->getBaz() == null ? null : new \ArrayObject($this->normalizer->normalize($object->getBaz(), 'json', $context), \ArrayObject::ARRAY_AS_PROPS);
}
foreach ($object as $key => $value) {
if (preg_match('/.*/', (string) $key)) {
$data[$key] = $value;
}
}
return $data;
} It does a double check if we're using OpenAPIv3 and nullable is not true on the object and the object isn't null, it makes more sense for OpenAPIv2 and OpenAPIv3 where nullable is true, I think it does cover all schemas. @Korbeil Let me know what you think? |
@Korbeil I've updated to handle the above and fixed the tests. If you agree with the changes I will squash the commits. |
Hey @Rid , You proposal for nullable handling seems totally fine to me. |
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.
✔️
Could you add a note in the CHANGELOG about this change ? 🙏 |
Fix empty objects by using `\ArrayObject` instead of plain arrays. Fix null objects by returning `null` when the value is `null`, this is becuase the `\ArrayObject` type cannot handle `null` values. Signed-off-by: Grant Millar <rid@cylo.io>
Ping @Korbeil |
@Rid looks like you need to rebase and solve the conflicts on this PR |
@thePanz happy to rebase, do you have merge permissions? |
no I don't, but this PR is marked as not mergeable by github due to merge-conflicts |
In order to fix #700, when we initialise an array, we first check if it should be a simple array or an arrayObject by checking the object type.
Further, when initialising variables in sub statements (for loops) which should be objects, we create arrayObjects.
example spec:
Old output:
New output: