Skip to content

[ModelConverters] java.lang.Object is an arbitrary type and should be mapped to an arbitrary type #3834

@ratoaq2

Description

@ratoaq2

The actual behavior:

  • Swagger model converter maps java.lang.Object to type: object
public class Foo {
        private Object bar;
   

    public static void main(String[] args) throws Exception {
        ResolvedSchema schema = ModelConverters.getInstance()
                .resolveAsResolvedSchema(
                        new AnnotatedType(Foo.class));

        String value = Json.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(schema.schema);

        System.out.println(value);
    }
 }

prints:

{
  "type" : "object"
}

If you map java.lang.Object to type: object in the OpenAPI spec, that means that schema only accepts an object with no properties. Using JSON representation, this would be the only acceptable value: "bar": {}.
But in the end "bar": 3 or "bar": "value-01" or "bar": true should also be acceptable, because they are possible values for that property in Java.

java.lang.Object means that it can be of any type (java.lang.String, java.lang.Integer, java.lang.Boolean, etc). java.lang.Object is an arbitrary type. And the way to represent that in OpenAPI spec is with an arbitrary type:
https://swagger.io/docs/specification/data-models/data-types/#any

A schema without a type matches any data type – numbers, strings, objects, and so on. {} is shorthand syntax for an arbitrary-type schema

The expected behavior:

  • It should have no type (shorthand) or to have the any type:
components:
  schemas:
    Foo:
      type: object
      properties:
        bar: {}

or

components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          anyOf:
          - type: string
          - type: number
          - type: integer
          - type: boolean
          - type: array
            items: {}
          - type: object

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