From 479c72e85e2cbb06f66669d2cf01d06802f601d6 Mon Sep 17 00:00:00 2001 From: Harrison Crosse Date: Fri, 24 May 2024 06:44:31 -0400 Subject: [PATCH] Add jsonschema export handling for empty subfields (#219) * Add handling for empty subfields * add test for empty object fix + update changelog --- CHANGELOG.md | 4 ++++ datacontract/export/jsonschema_converter.py | 2 +- tests/fixtures/local-json-complex/datacontract.yaml | 2 ++ tests/test_export_jsonschema.py | 8 ++++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed4cbbba..3d35bc03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `datacontract export --format dbml`: Export to [Database Markup Language (DBML)](https://dbml.dbdiagram.io/home/) (#135) +### Fixed + +- Fixed jsonschema export for models with empty object-typed fields (#218) + ## [0.10.4] - 2024-05-17 ### Added diff --git a/datacontract/export/jsonschema_converter.py b/datacontract/export/jsonschema_converter.py index d0f126bf..a17a5644 100644 --- a/datacontract/export/jsonschema_converter.py +++ b/datacontract/export/jsonschema_converter.py @@ -54,7 +54,7 @@ def to_property(field: Field) -> dict: property["unique"] = True if json_type == "object": # TODO: any better idea to distinguish between properties and patternProperties? - if next(iter(field.fields.keys())).startswith("^"): + if field.fields.keys() and next(iter(field.fields.keys())).startswith("^"): property["patternProperties"] = to_properties(field.fields) else: property["properties"] = to_properties(field.fields) diff --git a/tests/fixtures/local-json-complex/datacontract.yaml b/tests/fixtures/local-json-complex/datacontract.yaml index a88811a2..2ccf7218 100644 --- a/tests/fixtures/local-json-complex/datacontract.yaml +++ b/tests/fixtures/local-json-complex/datacontract.yaml @@ -52,6 +52,8 @@ models: type: string pattern: "^[0-9]{8}$" required: true + empty_object: + type: object examples: - type: json # csv, json, yaml, custom model: sts_data diff --git a/tests/test_export_jsonschema.py b/tests/test_export_jsonschema.py index 4d132353..604beeaa 100644 --- a/tests/test_export_jsonschema.py +++ b/tests/test_export_jsonschema.py @@ -158,6 +158,14 @@ def test_to_jsonschemas_complex_2(): "connection_test", "key_list" ] + }, + "empty_object": { + "type": [ + "object", + "null" + ], + "properties": {}, + "required": [] } }, "required": [