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

set schema name for EnumTypes

Open sam0r040 opened this issue 1 year ago • 4 comments

At Springwolf we use the parsed schemas to generate Examples. Thereby we use the field Schema.getName() for all schemas including enums.

When enumAsRef is used, the name of the enum is missing in its class schema. Also, when serialized, the name of a schema is ignored because of the @JsonIgnore annotation.

This PR adds the schema name for enums. We found the following comment: https://github.com/swagger-api/swagger-core/blob/5ee9cefe9455bf07d2671d9fd93fce2bd7c7b94f/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L379 And therefore believe that all enum classes should appear as models, resulting in the adaption of the tests.

Relates to #4722

sam0r040 avatar Aug 30 '24 14:08 sam0r040

Hi @sam0r040 could you share some details on how you used enumAsRef annotation ? I think it should work as intended

micryc avatar Sep 16 '24 11:09 micryc

Hi @micryc, sorry for the delay.

At Springwolf, we create AsyncAPI documentation for events, similar to OpenAPI/Swagger. @pdalfarr uses enumAsRef as shown in the following code example:

@io.swagger.v3.oas.annotations.media.Schema(enumAsRef = true)
    public enum MyEnumObject {
        VALUE1,
        VALUE2
    }

public record MyRootObject(MyEnumObject myEnumObjectField) {}

Our expectation of enumAsRef is that the enum schema is not embedded into the MyRootObject directly, but instead a ref to new MyEnumObject for the enum is created - including name, values etc. So, instead of inline, a ref is used.

We debugged the swagger-code and found the following two schemas when enumAsRef is used: Using: final Map<String, Schema> models = converters.readAll(MyRootObject.class);

First, MyRootObject

Schema
- name=MyRootObject
- type=object
- properties:
  - myEnumObjectField
    - Schema(name=myEnumObjectField)
      - ref=#/components/schemas/MyEnumObject

Second, MyEnumObject

Schema
- name=null <-- missing name, expected: name="MyEnumObject"
- type=string
- _enum=VALUE1,VALUE2

Original issue at Springwolf: https://github.com/springwolf/springwolf-core/issues/829#issuecomment-2263377913

sam0r040 avatar Oct 04 '24 14:10 sam0r040

Hi @sam0r040, I have tried to reproduce scenario with examples you provided and here is outcome:

MyEnumObject:
  type: string
  enum:
  - VALUE1
  - VALUE2
MyRootObject:
  type: object
  properties:
    myEnumObject:
      $ref: '#/components/schemas/MyEnumObject'

I don't see any unexpected behaviour in that case (I used latest swagger core 2.2.26)

micryc avatar Oct 10 '24 08:10 micryc

Hi @micryc, we're not looking at the rendered specification but at the pojos generated by io.swagger.v3.core.converter.ModelConverters.resolveAsResolvedSchema(...). For those, the name attribute will be null on the referenced enum object.

sam0r040 avatar Oct 11 '24 05:10 sam0r040