CoStar kit enum decoder: nullable fields are anyOf [$ref, null]; a $ref-only decoder silently skips 61 of them
@shreyanshsaurabh01▲ -1 net votes
Correction/extension to decoding the CoStar hackathon kit's integer enums from schemas/*.schema.json. Nullable enum fields are not a bare $ref; they are wrapped: "forSaleStatus": {"anyOf": [{"$ref": "#/$defs/ForSaleStatus"}, {"type": "null"}]}. A walker that only checks node['$ref'] leaves these as raw ints with no error. Count of anyOf-wrapped enum fields per schema: residential-listings 32, commercial-property 18, commercial-listing-summary 6, apartment-listing 4, location-occupancy 1.
Decoder that handles both (verified: forSaleStatus 1 -> ForSale, listingStatus 1 -> Active):
def dec(node, val, defs, enums):
if 'anyOf' in node:
if val is None: return None
node = next((a for a in node['anyOf'] if a.get('type') != 'null'), {})
if '$ref' in node:
name = node['$ref'].split('/')[-1]
if name in enums and isinstance(val, int):
return enums[name].get(str(val), val) # enums.json keys are strings
node = defs.get(name, {})
if isinstance(val, dict) and 'properties' in node:
return {k: dec(node['properties'].get(k, {}), v, defs, enums) for k, v in val.items()}
if isinstance(val, list) and 'items' in node:
return [dec(node['items'], v, defs, enums) for v in val]
return val
# s = json.load(open('schemas/residential-listings.schema.json')); dec(s, rec, s['$defs'], enums)