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

Generic fields in ParameterObject are not resolved #1128

Closed
orange-buffalo opened this issue Apr 4, 2021 · 2 comments
Closed

Generic fields in ParameterObject are not resolved #1128

orange-buffalo opened this issue Apr 4, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@orange-buffalo
Copy link

Consider the following use-case, where @ParameterObject refers to a class that extends a parametrized class:

open class AbstractParameterObject<T : Enum<T>> {

    var primitiveBaseField: Int? = null
    var genericField: T? = null
}

enum class ConcreteEnum {
    ONE,
    TWO
}

class ConcreteParameterObject : AbstractParameterObject<ConcreteEnum>() {
    var primitiveConcreteField: Int? = null
}

@RestController
@RequestMapping("/")
class HelloController {

    @GetMapping
    fun test(@ParameterObject requestData: ConcreteParameterObject): String = "hello"

}

Expected schema should contain the genericField definition, but it does not:

"parameters": [
  {
    "name": "primitiveConcreteField",
    "in": "query",
    "required": false,
    "schema": {
      "type": "integer",
      "format": "int32"
    }
  },
  {
    "name": "primitiveBaseField",
    "in": "query",
    "required": false,
    "schema": {
      "type": "integer",
      "format": "int32"
    }
  }
],

I reproduced the problem with a test in springdoc-openapi code base - please see the branch changes - and was able to get the field in the API definition. Unfortunately, the schema is still not generated properly. The issue is that at the moment when calculateSchema is called, the information about the original parameter class (ConcreteParameterObject from the example above) is already lost and we only have the declaring class for the getter (AbstractParameterObject). Obviously, there is not enough information to resolve the type information and wrong schema gets generated (or not at all for the enum case).

bnasslahsen added a commit that referenced this issue Apr 7, 2021
@bnasslahsen
Copy link
Contributor

bnasslahsen commented Apr 7, 2021

@orange-buffalo,

Thank for your search and contribution.
I have added your proposed change.

There is an error related to swagger-core: swagger-api/swagger-core#3858
A NPE is thrown by Jackson, in the swagger-core call from ModelResolver._addEnumProps(ModelResolver.java:944)

But i think, your fix should be working for other cases, and you have workarounds for enum inheritance.

You can also test this enhancement using the latest snapshot.

Please have a look at this commit and tell if it covers your case ?

@orange-buffalo
Copy link
Author

Hi @bnasslahsen, thank you for the solution. Yes, it covers the case. Hopefully once the Swagger issue is resolved, @Parameter(schema=@Schema(type = "string", allowableValues = {"ONE", "TWO"}) ) workaround can be removed and schema gets generated based on the types.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants