swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.39k stars 2.19k forks source link

Schema objects not outputting type when used with OpenAPI 3.1 #4587

Closed Yaytay closed 10 months ago

Yaytay commented 11 months ago

When requesting openapi.json I'm getting this:

  "components": {
    "schemas": {
      "AuthConfig": {
        "properties": {
          "name": {
            "type": "string"
          },
          "logo": {
            "type": "string"
          }
        }
      },

I'm expecting a type field under AuthConfig.

Putting this

logger.debug("{} {} ({} or {}): {}", schema.getClass(), schema.getName(), schema.getTypes(), schema.getType(), schema.getProperties() == null ? null : schema.getProperties().size());

in a ModelConverter outputs this:

class io.swagger.v3.oas.models.media.Schema AuthConfig (null or object): 2

I infer that types is null but type is set to "object".

Adding this:

schema.setTypes(ImmutableSet.builder().add("object").build());

to the ModelConverter makes the output the desired:

  "components": {
    "schemas": {
      "AuthConfig": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "logo": {
            "type": "string"
          }
        }
      },

Given that getType() returns the correct value I'm guessing that something in the serializer for OpenAPI 3.1 is explicitly calling getTypes and something more clever is needed to handle type vs types (set types explicitly when setType is called?).

frantuma commented 10 months ago

It is not clear how is the openapi obtained and how is it serialized, can you specify the original class(es), any config for swagger-core and how are you serializing it?

A common mistake is not setting the openapi31 flag when resolving an openapi spec out of code (via integration or using directly e.g. ModelResolver), another is using Yaml.prettyPrint() instead of Yaml31.prettyPrint()

frantuma commented 10 months ago

closing for now, please reopen with more details if you are still experiencing issues