Skip to content

Schema with missing type is implied as object if properties keyword is present #5719

@kletnoe

Description

@kletnoe

Environment: https://editor.swagger.io/
Version: 5.2.1

Assume there is a following specification:

openapi: 3.1.1
info:
  title: Sample API
  version: 0.0.1
components:
  schemas:
    user:
      description: User
      properties: {}

Swagger UI shows user schema type as object instead of any:
Image

It seems to me that Swagger contradicts OAS Specification.
Could you please clarify, is it a bug or am I mising something in the specs?
Maybe it is some kind of convention adopted in Swagger community?


I've checked both specifications - OAS 3.1.1 and Json Schema 2020-12.
It isn't stated anywhere that instance type should be implicitly implied as object in case properties keyword is present.

OAS Specification states the opposite:

JSON Schema keywords and format values operate on JSON “instances” which may be one of the six JSON data types, “null”, “boolean”, “object”, “array”, “number”, or “string”, with certain keywords and formats only applying to a specific type. For example, the pattern keyword and the date-time format only apply to strings, and treat any instance of the other five types as automatically valid. This means JSON Schema keywords and formats do NOT implicitly require the expected type. Use the type keyword to explicitly constrain the type.

If I do interpret it right, it means that properties keyword must apply to instances with type object. But it does not imply that the instance must be of type object. Strictly speaking, one could define the following fancy schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "minLength": 3
  "properties": {
    "id": {}
  },
  "required": ["id"],
  "items": {},
  "minItems": 1
}

And it would mean that the instance may be of any valid JSON type, but:

  • if it is a string - it must have at least 3 chars
  • if it is an object - it must have a field id
  • if it is an array - it must have at least one element.

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