|
| 1 | +import importlib.metadata |
1 | 2 | from datetime import datetime
|
2 | 3 |
|
3 | 4 | import pytest
|
|
10 | 11 | from .schemas import CustomList, CustomStringField
|
11 | 12 | from .utils import build_ref, get_schemas, validate_spec
|
12 | 13 |
|
| 14 | +MA_VERSION = Version(importlib.metadata.version("marshmallow")) |
| 15 | + |
13 | 16 |
|
14 | 17 | class TestMarshmallowFieldToOpenAPI:
|
15 | 18 | def test_fields_with_load_default_load(self, openapi):
|
@@ -133,6 +136,13 @@ class Meta:
|
133 | 136 | res = openapi.schema2jsonschema(WhiteStripesSchema)
|
134 | 137 | assert set(res["properties"].keys()) == {"guitarist", "drummer"}
|
135 | 138 |
|
| 139 | + def test_unknown_values_default_disallow(self, openapi): |
| 140 | + class UnknownDefaultSchema(Schema): |
| 141 | + first = fields.Str() |
| 142 | + |
| 143 | + res = openapi.schema2jsonschema(UnknownDefaultSchema) |
| 144 | + assert res["additionalProperties"] is False |
| 145 | + |
136 | 146 | def test_unknown_values_disallow(self, openapi):
|
137 | 147 | class UnknownRaiseSchema(Schema):
|
138 | 148 | class Meta:
|
@@ -163,6 +173,30 @@ class Meta:
|
163 | 173 | res = openapi.schema2jsonschema(UnknownExcludeSchema)
|
164 | 174 | assert "additionalProperties" not in res
|
165 | 175 |
|
| 176 | + @pytest.mark.parametrize("meta_unknown", (RAISE, INCLUDE, EXCLUDE, None)) |
| 177 | + @pytest.mark.parametrize( |
| 178 | + "instance_unknown,expected", ((RAISE, False), (INCLUDE, True), (EXCLUDE, None)) |
| 179 | + ) |
| 180 | + def test_unknown_values_instance_override_meta( |
| 181 | + self, openapi, instance_unknown, expected, meta_unknown |
| 182 | + ): |
| 183 | + class UnknownSchema(Schema): |
| 184 | + if meta_unknown is not None: |
| 185 | + |
| 186 | + class Meta: |
| 187 | + unknown = meta_unknown |
| 188 | + |
| 189 | + first = fields.Str() |
| 190 | + |
| 191 | + res = openapi.schema2jsonschema(UnknownSchema(unknown=instance_unknown)) |
| 192 | + if expected is None: |
| 193 | + assert "additionalProperties" not in res |
| 194 | + else: |
| 195 | + assert res["additionalProperties"] is expected |
| 196 | + |
| 197 | + @pytest.mark.skipif( |
| 198 | + MA_VERSION.major >= 4, reason="marshmallow 4 drop inferred fields" |
| 199 | + ) |
166 | 200 | def test_only_explicitly_declared_fields_are_translated(self, openapi):
|
167 | 201 | class UserSchema(Schema):
|
168 | 202 | _id = fields.Int()
|
|
0 commit comments