Skip to content
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

Fix json-schema type of objects nested under arrays. Fixes #400 #462

Merged
merged 2 commits into from
May 22, 2024

Conversation

tomgi
Copy link
Contributor

@tomgi tomgi commented Jun 20, 2023

Fix for #400

Objects nested under arrays are not neccesailry arrays themselves - the :member option shouldn't be passed down the AST when generating json-schema.

For example for the test schema

required(:roles).array(:hash) do
required(:name).value(:string, min_size?: 12, max_size?: 36)
required(:metadata).hash do
required(:assigned_at).value(:time)
end
end

the result of the fix is:

 {
   "$schema": "http://json-schema.org/draft-06/schema#",
   "type": "object",
   "properties": {
     "email": {
       "type": "string"
     },
     "age": {
       "type": "integer"
     },
     "roles": {
       "type": "array",
       "items": {
         "type": "object",
         "properties": {
           "name": {
             "type": "string",
             "minLength": 12,
             "maxLength": 36
           },
           "metadata": {
-            "type": "array",
-            "items": {
-              "type": "object",
-              "properties": {
-                "assigned_at": {
-                  "type": "string",
-                  "format": "time"
-                }
-              },
-              "required": [
-                "assigned_at"
-              ]
-            }
+            "type": "object",
+            "properties": {
+              "assigned_at": {
+                "format": "time",
+                "type": "string"
+              }
+            },
+            "required": [
+              "assigned_at"
+            ]
           }
         },
         "required": [
           "name",
           "metadata"
         ]
       }
     },
     "address": {
       "type": "object",
       "properties": {
         "street": {
           "type": "string"
         }
       },
       "required": [
 
       ]
     },
     "id": {
       "anyOf": [
         {
           "type": "string"
         },
         {
           "type": "integer"
         }
       ]
     }
   },
   "required": [
     "email",
     "roles",
     "id"
   ]
 }

@tomgi tomgi changed the title Fix type of objects nested under arrays. Fixes #400 Fix json-schema type of objects nested under arrays. Fixes #400 Jun 20, 2023
@cpgo
Copy link
Contributor

cpgo commented Jan 3, 2024

Hey @tomgi just asked about this issue on the zulip chat.
Tried monkey patching your solution on my project and it seems to be working perfectly.

@solnic is there any plans to merge this PR?

@shauns
Copy link

shauns commented May 13, 2024

Following up on this -- @solnic would merging it be possible? The bug is still in place and this fix is a good one.

@solnic solnic merged commit 8f9c073 into dry-rb:main May 22, 2024
10 checks passed
@solnic
Copy link
Member

solnic commented May 22, 2024

I'm sorry it took so long, I'll push a release, thanks for addressing it

@lukelex
Copy link

lukelex commented May 23, 2024

Awesome work. Thanks for dedicating your time to fix this ❤️

@tomgi tomgi deleted the fix_nested_array_object branch May 27, 2024 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants