notEthan / jsi

JSI: JSON Schema Instantiation in Ruby
Other
22 stars 1 forks source link

type coercion of attributes #236

Open notEthan opened 2 years ago

notEthan commented 2 years ago

Base#[] could coerce children's types, based on format keyword or other indications of a more natural representation. e.g.:

more thoughts:

spacether commented 1 year ago

So I think that type coercion is only a good idea for accessors because it sounds like your instances subclass all validated schemas, right? If so then you need to allow for non format types to also validate the instance payload. Because you can have

allof:
- type: string
- type: string
  format: date

In python-experimental I make instances subclass all validated schemas and provide acessor methods for format data types. One can see an example of the accessors here: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python-experimental/tests_manual/test_decimal_payload.py#L31

And one also needs to allow format to be applied on AnyType schemas like in: https://github.com/OpenAPITools/openapi-generator/pull/13360

notEthan commented 1 year ago

Hi Justin, good to hear from you.

only a good idea for accessors

I'm not sure how you mean this. Do you mean object property accessors? I think this would apply equally to any instance.

instances subclass all validated schemas

JSI instances are instances of mixins corresponding to every schema which describes them. This doesn't depend on validation, mostly, e.g. items and properties schemas apply to child instances whether or not they validate (though conditional application like with oneOf or contains is more complicated).

(This isn't really on topic related to type coersion, but worth clarifying I think.)

acessor methods for format data types

This might be the way to go. So far this is just some thoughts thrown together in an issue that I might implement eventually, so haven't figured out what kind of interface would work, allowing access to coerced vs. raw instance data.

And one also needs to allow format to be applied on AnyType schemas like in: OpenAPITools/openapi-generator#13360

I'm not sure what an AnyType schema means, and at a glance I'm not sure what that PR is doing (far too large for me to grok ... or even to tell what's generated code vs the actual origin of all the changes).

spacether commented 1 year ago

An AnyType schema is an empty schema {} More than just object property accessors. An accessor method on deserializsd schema instances. Methods like as_date as_datetime, as_decimal etc.

Here is an example of the accessor method as_decimal_oapg that converts a string into a Decimal

        m = DecimalPayload('12')
        assert isinstance(m, DecimalPayload)
        assert isinstance(m, DecimalSchema)
        assert isinstance(m, str)
        assert m == '12'
        assert m.as_decimal_oapg == decimal.Decimal('12')
spacether commented 1 year ago

I was trying to point out these type unset (AnyType) + format example in my above PR

    AnyTypeAndFormat:
      type: object
      properties:
        uuid:
          format: uuid
        date:
          format: date
        date-time:
          format: date-time
        number:
          format: number
        binary:
          format: binary
        int32:
          format: int32
        int64:
          format: int64
        double:
          format: double
        float:
          format: float