You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "ASF subversion and git services (Jira)" <ji...@apache.org> on 2023/01/03 13:32:00 UTC

[jira] [Commented] (AVRO-3229) Python Avro doesn't validate the default value of an enum field

    [ https://issues.apache.org/jira/browse/AVRO-3229?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17654004#comment-17654004 ] 

ASF subversion and git services commented on AVRO-3229:
-------------------------------------------------------

Commit 3545c1bb716b6a21617be52c6153c6be160d75fe in avro's branch refs/heads/master from Tommi Vainikainen
[ https://gitbox.apache.org/repos/asf?p=avro.git;h=3545c1bb7 ]

AVRO-3229: Raise on invalid enum default only if validation enabled (#2039)

AVRO-3229 added validation for enum default raising an exception
for invalid default.  Allow disabling validation, and parsing enums
with enum default not defined using existing `validate_enum_symbols`
flag.

> Python Avro doesn't validate the default value of an enum field
> ---------------------------------------------------------------
>
>                 Key: AVRO-3229
>                 URL: https://issues.apache.org/jira/browse/AVRO-3229
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: python
>    Affects Versions: 1.10.2
>         Environment: python --version
> Python 3.9.5
> pip freeze | grep avro
> avro==1.10.2
>            Reporter: Augusto Hack
>            Assignee: Michael A. Smith
>            Priority: Trivial
>              Labels: pull-request-available
>             Fix For: 1.11.1
>
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> The following schema is invalid for Java (it fails to compile), because the default value is not a valid symbol:
>  
> {code:java}
> {
>   "type": "record",
>   "name": "test_schema",
>   "fields": [
>     {
>       "name": "test_enum",
>       "type": {
>         "name": "test_enum_type",
>         "type": "enum",
>         "symbols": [
>           "NONE"
>         ],
>         "default": "UNKNOWN"
>       }
>     }
>   ]
> }
> {code}
> This matches the behavior documented in the spec:
>  
> {quote}default: A default value for this enumeration, used during resolution when the reader encounters a symbol from the writer that isn't defined in the reader's schema (optional). The value provided here must be a JSON *string that's a member of the symbols array*. See documentation on schema resolution for how this gets used.
> {quote}
> But the same schema is silently accepted by the python library (although the writer doesn't allow the invalid value to be produced):
> {code:java}
> import avro.schema
> from avro.datafile import DataFileReader, DataFileWriter
> from avro.io import DatumReader, DatumWriter
> with open("test.avsc", "rb") as handler:
>     schema = avro.schema.parse(handler.read())
> DATA_FILE = "test.avro"
> with open(DATA_FILE, "wb") as handler:
>     writer = DataFileWriter(handler, DatumWriter(), schema)
>     writer.append({"test_enum": "NONE"})
>     # writer.append({"test_enum": "UNKNOWN"})
>     # writer.append({})
>     writer.close()
> with open(DATA_FILE, "rb") as handler:
>     for user in DataFileReader(handler, DatumReader()):
>         print(user)
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)