ta1meng commented on issue #9571:
URL: https://github.com/apache/pulsar/issues/9571#issuecomment-828076474


   I went deeper today and now believe that the original issue, in its exact 
form, was resolved by #9612. 
   
   Here are all the cases I got working, including workarounds where I 
encountered bugs.
   
   All schemas were uploaded through pulsar-admin. Producers and consumers were 
disallowed from updating schemas.
   
   CAVEAT: Pulsar does not support `"default": null` fully. The workaround is 
to remove it from any Avro schema that contains it. When `"default": null` is 
removed, the resulting behavior is the same as if it were specified. 
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Latitude\",\"type\":\"record\",\"fields\":[{\"name\":\"latitude\",\"type\":\"float\"}]}",
       "properties": {}
   }
   class Latitude(Record):
      latitude = Float(required=True)
   ```
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Latitude\",\"type\":\"record\",\"fields\":[{\"name\":\"latitude\",\"type\":\"float\"},{\"name\":\"longitude\",\"type\":\"float\"}]}",
       "properties": {}
   }
   class Latitude(Record):
      latitude = Float(required=True)
      longitude = Float(required=True)
   ```
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Latitude\",\"type\":\"record\",\"fields\":[{\"name\":\"latitude\",\"type\":\"float\"},{\"name\":\"longitude\",\"type\":\"float\"},{\"name\":\"fieldId\",\"type\":\"string\"},{\"name\":\"action\",\"type\":[\"null\",\"string\"]}]}",
       "properties": {}
   }
   class Latitude(Record):
      latitude = Float(required=True)
      longitude = Float(required=True)
      fieldId = String(required=True)
      action = String(required=False)
   ```
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Location\",\"type\":\"record\",\"fields\":[{\"name\":\"fieldId\",\"type\":\"string\"}]}",
       "properties": {}
   }
   class Location(Record):
      fieldId = String(required=True)
   ```
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Location\",\"type\":\"record\",\"fields\":[{\"name\":\"fieldId\",\"type\":\"string\"},{\"name\":\"address\",\"type\":\"string\"}]}",
       "properties": {}
   }
   class Location(Record):
      fieldId = String(required=True)
      address = String(required=True)
      @classmethod
      def schema(cls):
          schema = {
              'name': str(cls.__name__),
              'type': 'record',
              'fields': []
          }
   
          # Do NOT sort the keys!!
          for name in cls._fields.keys():
              field = cls._fields[name]
              field_type = field.schema() if field._required else ['null', 
field.schema()]
              schema['fields'].append({
                  'name': name,
                  'type': field_type
              })
          return schema
   ```
   
   ```
   {
       "type": "AVRO",
       "schema": 
"{\"name\":\"Location\",\"type\":\"record\",\"fields\":[{\"name\":\"fieldId\",\"type\":\"string\"},{\"name\":\"address\",\"type\":\"string\"},{\"name\":\"action\",\"type\":[\"string\",\"null\"],\"default\":\"planting\"}]}",
       "properties": {}
   }
   class Location(Record):
      fieldId = String(required=True)
      address = String(required=True)
      action = String(required=False, default='planting')
      @classmethod
      def schema(cls):
          schema = {
              'name': str(cls.__name__),
              'type': 'record',
              'fields': []
          }
   
          # Do NOT sort the keys!!
          for name in cls._fields.keys():
              field = cls._fields[name]
              field_type = field.schema() if field._required else (
                  # HACK for default values
                  [field.schema(), 'null'] if field._default != None else 
['null', field.schema()]
              )
              schema['fields'].append({
                  'name': name,
                  'type': field_type
              } if field._default == None else {
                  # HACK for default values
                  'name': name,
                  'type': field_type,
                  'default': field._default
              })
          return schema
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to