[13.x] Stringify properties keys in JsonSchema Serializer#60265
Closed
sulimanbenhalim wants to merge 1 commit into
Closed
[13.x] Stringify properties keys in JsonSchema Serializer#60265sulimanbenhalim wants to merge 1 commit into
sulimanbenhalim wants to merge 1 commit into
Conversation
Member
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I am building a small EdTech app where teachers can create multiple choice quizes and an AI grades the student submissions. I use Laravel JsonSchema to describe what a valid answer payload looks like, then I run it through opis/json-schema before sending the answer to the AI grader.
The quiz has answer slots A, B, C, D. In the payload the slots are indexed by position so slot A is
"0", slot B is"1", etc. When I tried to publish my first quiz the validator kept saying my schema is invalid and every submisison was getting rejectedHere is what I had:
When I encode this to JSON, the
selected_slots.propertiesfield comes out as an array, not an object:opis throws
properties must be an object. The JSON Schema spec (Draft 2020-12, the section about thepropertieskeyword) also says it must be an object, so this output is not valid https://json-schema.org/understanding-json-schema/reference/object#propertiesPR #60149 was merged last week and it handles the same coercion issue for the
requiredarray. But the same logic was not applied topropertiesitself. The test in that PR uses keys"1"and"4"which PHP coerces to int 1 and 4. Those are not list shaped so json_encode still emits an object and the bug stays hidden. With keys like"0", "1", "2"the array becomes a php list starting from 0 and json_encode emits an array instead.Fix
PHP coerces numeric string keys to int and there is no way to keep them as strings in an array. The cleanest way I found to make json_encode emit an object is to cast the array to
stdClass, but only when it would otherwise be encoded as a list. For all normal cases (string keys like'name','email') nothing changes and the existing tests pass without modification.Test
I added a test next to the one from #60149 that asserts the JSON output keeps
propertiesas an object when the keys are"0", "1", "2".Thanks!