godatadriven / pydantic-spark

MIT License
25 stars 8 forks source link

Add support for optional objects in lists + Add support for optional lists of (optional) objects #43

Open chidifrank opened 8 months ago

chidifrank commented 8 months ago

Adds support for optional objects in lists & Add support for optional lists of (optional) objects

  1. Generating the spark schema for array columns that contain optional objects used to result in errors.

The script would try to get the items for array types but if you have an array with optional object elements it would result in a NotImplementedError since the type and ref would be None. The ref would reside in anyOf which would contain $ref

class OptionalObjectsInArray(SparkBase):
    c1: List[Optional[Nested2Model]]

OptionalObjectsInArray.spark_schema()
> `NotImplementedError`
  1. Generating the spark schema for optional array columns that contain (optional) objects used to result in an Attribute error, since the array field would not have the attribute items it would have the attribute anyOf which would contain items
class OptionalArrayOfArraysWithOptionalObjectsInArray(SparkBase):
    c1: Optional[List[OptionalObjectsInArray]]

OptionalArrayOfArraysWithOptionalObjectsInArray.spark_schema()
> `AttributeError: 'NoneType' object has no attribute 'get'`
arcaputo3 commented 7 months ago

This is a fantastic change; currently installing from this branch! Please merge :)