Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make enum parsing fall back to default value in schema #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ private Object ensureEnum(Schema schema, Object value, Deque<String> path) {
if (symbols.contains(value)) {
return new GenericData.EnumSymbol(schema, value);
}
else if (schema.getEnumDefault() != null) {
return new GenericData.EnumSymbol(schema, schema.getEnumDefault());
}
throw enumException(path, symbols.stream().map(String::valueOf).collect(joining(", ")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,45 @@ class JsonAvroConverterSpec extends Specification {
exception.cause.message ==~ /.*enum type and be one of A, B, C.*/
}

def 'should fallback to enum default when passing an invalid enum type'() {
given:
def schema = '''
{
"name": "testSchema",
"type": "record",
"fields": [
{
"name" : "field_enum",
"type" : {
"name" : "MyEnums",
"type" : "enum",
"symbols" : [ "A", "B", "C" ],
"default" : "B"
}
}
]
}
'''

def json = '''
{
"field_enum": "D"
}
'''

def jsonAfterParse = '''
{
"field_enum": "B"
}
'''

when:
def result = converter.convertToJson(converter.convertToAvro(json.bytes, schema), schema)

then:
toMap(jsonAfterParse) == toMap(result)
}

def 'should accept null when value can be of any nullable array/map type'() {
given:
def schema = '''
Expand Down