Why does EK9 reject duplicate properties in records with JSON support?
← Code Quality · Ref: Q829
When a record uses 'default operator' to auto-generate operators including $$ (toJSON), EK9 requires all property names to be unique across the inheritance hierarchy. Duplicate property names would produce ambiguous JSON keys.
THE RULE
If a child record redeclares a parent property AND uses 'default operator':
ParentRecord
name as String
default operator
ChildRecord extends ParentRecord
name as String // ERROR: E02020 — ambiguous JSON key
default operator
WHY THIS MATTERS
The $$ operator generates JSON like {"name": "value"}. If both parent and child have 'name', the JSON would have duplicate keys — which is undefined behaviour in JSON.
HOW TO FIX
- Use a different name in the child: childName as String
- Or manually implement the $$ operator instead of using default
See Q819 for duplicate property basics. See Q97 for records.
Example
defines module qa.quality.duplicate.json defines record BaseConfig as open label as String: String() default operator ? default operator $ ChildConfig extends BaseConfig childLabel as String: String() default operator ? default operator $ defines program DuplicateJsonDemo() stdout <- Stdout() child <- ChildConfig() stdout.println($child)
Common mistakes
E02010 — The parent BaseConfig already has 'label'. Redeclaring it in a child creates a duplicate property. Use a different name. See ek9 -h E02010 for details.
Incorrect:
ChildConfig extends BaseConfig label as String: String()
Correct:
ChildConfig extends BaseConfig childLabel as String: String()
Other ways to ask this
- What is E02020 CANNOT_SUPPORT_TO_JSON_DUPLICATE_PROPERTY_FIELD?
- Why can't my child record redeclare a parent property with default operators?
- How does property duplication affect JSON serialization in EK9?
Coming from another language?
Java: Jackson serializes duplicate fields from parent+child creating ambiguous JSON. Python: no enforcement. Rust: no inheritance. Go: embedded struct fields can shadow. EK9: compile-time error prevents ambiguous JSON serialization.
Keywords: property, duplicate, json, E02020, record, serialization, toJson