Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ jobs:
distribution: 'temurin'
java-version: '17'

- name: Test spec normalization
run: |
python3 -m pip install --quiet --break-system-packages pyyaml
python3 -m unittest discover -s scripts -p 'test_*.py'

- name: Build
run: mvn compile -q
6 changes: 6 additions & 0 deletions scripts/normalize-spec-for-java.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def collapse_unexpressible_unions(node, path, collapsed):
if not isinstance(node, dict):
return

items = node.get("items")
if node.get("uniqueItems") and isinstance(items, dict) and "enum" in items:
# native Java's Set<Enum> addItem helper constructs HashSet<String>.
# Use List<Enum>; the API still enforces uniqueness on the wire.
del node["uniqueItems"]

for keyword in COMPOSED_KEYWORDS:
branches = node.get(keyword)
if not isinstance(branches, list):
Expand Down
27 changes: 27 additions & 0 deletions scripts/test_normalize_spec_for_java.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import importlib.util
import unittest
from pathlib import Path

spec = importlib.util.spec_from_file_location(
'normalize', Path(__file__).with_name('normalize-spec-for-java.py')
)
normalize = importlib.util.module_from_spec(spec)
spec.loader.exec_module(normalize)


class EnumArrayTest(unittest.TestCase):
def test_enum_arrays_use_lists_without_losing_enum_values(self):
schema = {'type': 'array', 'uniqueItems': True,
'items': {'type': 'string', 'enum': ['whatsapp', 'messenger']}}
normalize.collapse_unexpressible_unions(schema, '$', [])
self.assertNotIn('uniqueItems', schema)
self.assertEqual(schema['items']['enum'], ['whatsapp', 'messenger'])

def test_non_enum_arrays_keep_set_semantics(self):
schema = {'type': 'array', 'uniqueItems': True, 'items': {'type': 'string'}}
normalize.collapse_unexpressible_unions(schema, '$', [])
self.assertTrue(schema['uniqueItems'])


if __name__ == '__main__':
unittest.main()
Loading