dry-schema icon indicating copy to clipboard operation
dry-schema copied to clipboard

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

Open tomgi opened this issue 2 years ago • 1 comments

Fix for https://github.com/dry-rb/dry-schema/issues/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 https://github.com/dry-rb/dry-schema/blob/5853103d6849490ae8ba94158f73631c1fd121a8/spec/extensions/json_schema/schema_spec.rb#L28-L34

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 avatar Jun 20 '23 04:06 tomgi

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?

cpgo avatar Jan 03 '24 14:01 cpgo

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

shauns avatar May 13 '24 10:05 shauns

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

solnic avatar May 22 '24 20:05 solnic

Awesome work. Thanks for dedicating your time to fix this :heart:

lukelex avatar May 23 '24 08:05 lukelex