Skip to content

Commit 646eba5

Browse files
committed
use anyOf when multiple types are found
1 parent 3cbe7ca commit 646eba5

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

singer/schema_generation.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ def add_observations(acc, path, data):
5959
return acc
6060

6161
def to_json_schema(obs):
62-
result = {'type': ['null']}
63-
62+
types = []
6463
for key in obs:
6564

65+
result = {'type': ['null']}
66+
6667
if key == 'object':
6768
result['type'] += ['object']
6869
if 'properties' not in result:
@@ -77,6 +78,7 @@ def to_json_schema(obs):
7778
elif key == 'date':
7879
result['type'] += ['string']
7980
result['format'] = 'date-time'
81+
8082
elif key == 'string':
8183
result['type'] += ['string']
8284

@@ -97,7 +99,15 @@ def to_json_schema(obs):
9799
else:
98100
raise Exception("Unexpected data type " + key)
99101

100-
return result
102+
types.append(result)
103+
104+
if len(types) == 0:
105+
return {'type': ['null', 'string']}
106+
107+
if len(types) == 1:
108+
return types[0]
109+
110+
return {'anyOf': types}
101111

102112
def generate_schema(records):
103113
obs = {}

0 commit comments

Comments
 (0)