Repository: Fatal1ty/mashumaro (language: python)

Issue:
Allow customizing additionalProperties in jsonschemas
**Is your feature request related to a problem? Please describe.**
We have some classes for which we set additionalProperties=True in the jsonschema. Would it be possible to allow setting that in the embedded Config class?

**Describe the solution you'd like**
Also accept an additionalProperties key when the "properties" key is set from json_schema in the Config

**Describe alternatives you've considered**
Can set it directly in the jsonschema object, but we have a lot of nested classes so this gets messay.




Discussion / hints:
Will it suit you if we support overriding "additionalProperties" in Config?

```python
@dataclass
class MyClass(DataClassDictMixin):
    foo: str

    class Config(BaseConfig):
        json_schema = {
            "additionalProperties": True,
        }
```

```json
{
    "type": "object",
    "title": "MyClass",
    "properties": {
        "foo": {
            "type": "string"
        }
    },
    "additionalProperties": true,
    "required": [
        "foo"
    ]
}
```

> Also accept an additionalProperties key when the "properties" key is set from json_schema in the Config

Please note that the "properties" key is currently used only for overriding schemas for existing dataclass fields, so if you add additional keys in "properties", they will be ignored. Let me know if you need to have additional keys added to JSON Schema. If this is a case, I may be wrong but setting "additionalProperties" to True would seem to be redundant.
@gshank

Thank you by the way for your contribution to the development of JSON Schema functionality. I would also like to wait for you to gather as much as possible JSON Schema issues to work on them together for the following release.
Yes, the way you're suggesting is fine. That's what I've done locally to get it working. I'm making local changes to mashumaro to get it all working so that I can provide tickets for everything. I have the unit tests working, and now I'm going to work on the functional tests. I'll update the other ticket with what I've done for that...

Fix patch:
diff --git a/README.md b/README.md
index d1287b97..f6e11e62 100644
--- a/README.md
+++ b/README.md
@@ -2698,7 +2698,9 @@ Object constraints:
 ### Extending JSON Schema
 
 Using a `Config` class it is possible to override some parts of the schema.
-Currently, it works for dataclass fields via "properties" key:
+Currently, you can do the following:
+* override some field schemas using the "properties" key
+* change `additionalProperties` using the "additionalProperties" key
 
 ```python
 from dataclasses import dataclass
@@ -2716,7 +2718,8 @@ class FooBar:
                     "type": "string",
                     "description": "bar"
                 }
-            }
+            },
+            "additionalProperties": True,
         }
 
 print(build_json_schema(FooBar).to_json())
@@ -2738,7 +2741,7 @@ print(build_json_schema(FooBar).to_json())
             "type": "integer"
         }
     },
-    "additionalProperties": false,
+    "additionalProperties": true,
     "required": [
         "foo",
         "bar"
@@ -2747,6 +2750,9 @@ print(build_json_schema(FooBar).to_json())
 ```
 </details>
 
+You can also change the "additionalProperties" key to a specific schema
+by passing it a `JSONSchema` instance instead of a bool value.
+
 ### JSON Schema and custom serialization methods
 
 Mashumaro provides different ways to override default serialization methods for
diff --git a/mashumaro/jsonschema/schema.py b/mashumaro/jsonschema/schema.py
index 65c3ec32..9bce0429 100644
--- a/mashumaro/jsonschema/schema.py
+++ b/mashumaro/jsonschema/schema.py
@@ -285,15 +285,16 @@ def override_field_instance_type_if_needed(
 def on_dataclass(instance: Instance, ctx: Context) -> Optional[JSONSchema]:
     # TODO: Self references might not work
     if is_dataclass(instance.origin_type):
+        jsonschema_config = instance.get_config().json_schema
         schema = JSONObjectSchema(
             title=instance.origin_type.__name__,
-            additionalProperties=False,
+            additionalProperties=jsonschema_config.get(
+                "additionalProperties", False
+            ),
         )
         properties: Dict[str, JSONSchema] = {}
         required = []
-        field_schema_overrides = instance.get_config().json_schema.get(
-            "properties", {}
-        )
+        field_schema_overrides = jsonschema_config.get("properties", {})
         for f_name, f_type, has_default, f_default in instance.fields():
             override = field_schema_overrides.get(f_name)
             f_instance = instance.copy(type=f_type, name=f_name)


Test patch:
diff --git a/tests/test_jsonschema/test_jsonschema_generation.py b/tests/test_jsonschema/test_jsonschema_generation.py
index 626f9776..0b088333 100644
--- a/tests/test_jsonschema/test_jsonschema_generation.py
+++ b/tests/test_jsonschema/test_jsonschema_generation.py
@@ -1051,3 +1051,45 @@ class DataClass:
         .items.to_dict()
         == schema
     )
+
+
+def test_jsonschema_with_additional_properties_true():
+    @dataclass
+    class DataClass:
+        x: int
+
+        class Config(BaseConfig):
+            json_schema = {"additionalProperties": True}
+
+    schema = JSONObjectSchema(
+        title="DataClass",
+        properties={
+            "x": JSONSchema(type=JSONSchemaInstanceType.INTEGER),
+        },
+        additionalProperties=True,
+        required=["x"],
+    )
+    assert build_json_schema(DataClass) == schema
+
+
+def test_jsonschema_with_additional_properties_schema():
+    @dataclass
+    class DataClass:
+        x: int
+
+        class Config(BaseConfig):
+            json_schema = {
+                "additionalProperties": JSONSchema(
+                    type=JSONSchemaInstanceType.INTEGER
+                )
+            }
+
+    schema = JSONObjectSchema(
+        title="DataClass",
+        properties={
+            "x": JSONSchema(type=JSONSchemaInstanceType.INTEGER),
+        },
+        additionalProperties=JSONSchema(type=JSONSchemaInstanceType.INTEGER),
+        required=["x"],
+    )
+    assert build_json_schema(DataClass) == schema