Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed cannot generate schema for response having a collection of enums (Groovy) #1137

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,11 @@ protected Schema resolveSchema(OpenAPI openAPI, @Nullable Element definingElemen
Boolean isIterable = null;

ClassElement componentType = type != null ? type.getFirstTypeArgument().orElse(null) : null;
if (componentType != null && !componentType.isIterable() && !componentType.isArray()) {
// bug in micronaut-core, see "test build OpenAPI for set of enums"
// type.getFirstTypeArgument() return ClassElement, but must be EnumElement
componentType = context.getClassElement(componentType.getName()).orElse(componentType);
}
if (type instanceof WildcardElement) {
WildcardElement wildcardEl = (WildcardElement) type;
type = CollectionUtils.isNotEmpty(wildcardEl.getUpperBounds()) ? wildcardEl.getUpperBounds().get(0) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,83 @@ class MyBean {}
!schema.required.contains('battingAverage')
}

@Issue("https://github.com/micronaut-projects/micronaut-openapi/issues/1135")
void "test build OpenAPI for set of enums"() {
given: "An API definition"
when:
buildBeanDefinition('test.MyBean', '''
package test

import groovy.transform.CompileStatic
import io.micronaut.core.annotation.Nullable
import io.micronaut.http.HttpResponse
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.swagger.v3.oas.annotations.media.Schema

@CompileStatic
@Controller
class DogController {

@Get('/dog')
HttpResponse<Dog> get() {
Dog dog = new Dog(
name: 'Rex', breed: Breed.GERMAN_SHEPHERD,
traits: EnumSet.of(Trait.QUICKNESS, Trait.COURAGE, Trait.LOYALTY)
)

return HttpResponse.ok(dog)
}
}

@CompileStatic
class Dog {

String name
Breed breed
Set<Trait> traits
}

@CompileStatic
enum Breed {

GERMAN_SHEPHERD,
BULLDOG,
GOLDEN_RETRIEVER
}

@CompileStatic
enum Trait {

QUICKNESS,
COURAGE,
LOYALTY
}

@jakarta.inject.Singleton
class MyBean {}
''')
then: "the state is correct"
Utils.testReference != null

when: "The OpenAPI is retrieved"
OpenAPI openAPI = Utils.testReference
Schema breedSchema = openAPI.components.schemas['Breed']
Schema dogSchema = openAPI.components.schemas['Dog']
Schema traitSchema = openAPI.components.schemas['Trait']

then: "the components are valid"
breedSchema
dogSchema
traitSchema

dogSchema.properties.traits
dogSchema.properties.traits.type == 'array'
dogSchema.properties.traits.items.$ref == '#/components/schemas/Trait'

traitSchema.type == 'string'
traitSchema.enum.get(0) == 'QUICKNESS'
traitSchema.enum.get(1) == 'COURAGE'
traitSchema.enum.get(2) == 'LOYALTY'
}
}