openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

properties' custom_formatters don't work unless the containing shema is strictly Object

Open phrfpeixoto opened this issue 6 years ago • 2 comments

Found this issue while investigating the code for #147

I noticed that custom_formatters are not being user for unmarshalling and validating properties, unless their containing Schema is strictly defined.

That is, if the Schema is of type SchemaType.ANY, custom_validators will not work. That's because the _unmarshal_any object fails to propagate the attributes to get_cast_mapping:

def _unmarshal_any(self, value, custom_formatters=None, strict=True):
    types_resolve_order = [
        SchemaType.OBJECT, SchemaType.ARRAY, SchemaType.BOOLEAN,
        SchemaType.INTEGER, SchemaType.NUMBER, SchemaType.STRING,
    ]
    cast_mapping = self.get_cast_mapping()

    ...

This is specifically sensitive for defining MediaType objects:

paths:
  /test:
    get:
      operationId: test
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/DefaultResponse'
components:
  schemas:
    DefaultResponse:
      type: object
      properties:
        url:
          type: string
          format: url

The shema type for that MediaType will be Any (See #147), so that 'url' format will not unmarshal, even though you may define a custom formatter.

phrfpeixoto avatar Jul 07 '19 19:07 phrfpeixoto

As a work around, you may add a type declaration alongside that allOf:

paths:
  /test:
    get:
      operationId: test
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                type: object
                allOf:
                  - $ref: '#/components/schemas/DefaultResponse'
components:
  schemas:
    DefaultResponse:
      type: object
      properties:
        url:
          type: string
          format: url

phrfpeixoto avatar Jul 07 '19 23:07 phrfpeixoto

@p1c2u Would you mind reviewing this?

phrfpeixoto avatar Jul 14 '19 03:07 phrfpeixoto