Skip to content

Support arbitrary-type schema (schema without a type) #4014

@mjustin

Description

@mjustin

As indicated by the OpenAPI documentation, the type keyword can be excluded to match any data type.

A schema without a type matches any data type – numbers, strings, objects, and so on.

However, from what I can tell, there isn't a way to actually achieve this in Swagger Core. Even if a Schema is manually created with a null type (e.g. using a custom ModelConverter), Swagger Core will automagically use type object for it. This is decidedly not equivalent to the arbitrary type, as it will not match string, number, integer, boolean, or array types.

Tracing through the code, this appears to be due to ModelResolver using Jackson to clone the Schema, which due to the implementation in ModelDeserializer, turns the null type back into the object type.

property = Json.mapper().readValue(Json.pretty(property), Schema.class);

} else { // assume object
schema = deserializeObjectSchema(node);
}

Note that the equivalent type used in the documentation cannot be used either, because the arbitrary type is recursive — it is also the type of the items in the array. That is to say the arbitrary type could be an array containing arbitrary type items, which could also be arrays containing arbitrary type items, and so on.

components:
  schemas:
    AnyValue:
      anyOf:
        - type: string
        - type: number
        - type: integer
        - type: boolean
        - type: array
          items: {}
        - type: object

Related to this issue is #3834, which could not be implemented without having a way of supporting the arbitrary type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions