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

Swagger 2's default and allowableValues do not consider the property's type #455

Open
Nephery opened this issue Jul 9, 2024 · 3 comments
Labels
wontfix This will not be worked on

Comments

@Nephery
Copy link
Contributor

Nephery commented Jul 9, 2024

For the following Swagger 2 module properties:

From @Schema(allowableValues = …) on fields/methods, derive its "const"/"enum".
From @Schema(defaultValue = …) on fields/methods, derive its "default".

The outputted JSON always treats them as Strings, and does not consider the actual type of the property.

For example, if I have the following:

public class TestSchema {
	@Schema(allowableValues = "true")
	public boolean myRestrictedBoolean;
}

This would produce a schema like:

{
  "type" : "object",
  "properties" : {
    "myRestrictedBoolean" : {
      "type" : "boolean",
      "const" : "true"
    }
  }
}

Instead of "const" : "true", I would have expected this to be "const" : true (i.e. not a String value). The same happens for any other primitive types.

@CarstenWickner
Copy link
Member

Hi @Nephery,

Yeah, that's the downside of the Swagger annotation providing everything as String values.
We could add extra smarts to cover some straightforward scenarios such as this one, but to be honest, I'd expect something like the jakarta @AssertTrue validation annotation to be present in this case, which allows enforcement and can be picked up without having to parse a String.

I'm open to a PR though, if you deem it worthwhile.
I fully agree, that the schema currently being generated there is invalid. The issue can be avoided by not using the @Schema(allowableValues = "true") in such a case and/or a fix is required here.

@Nephery
Copy link
Contributor Author

Nephery commented Jul 11, 2024

That's fair.

I raised #456 to add support for Jakarta @AssertTrue and @AssertFalse.

Maybe I'll come back around in the future to try and raise a PR to add some support for primitive types in @Schema(allowableValues = "true"). But adding support for Jakarta @AssertTrue and @AssertFalse does actually solve my immediate issue. So that's good enough for me for now. 🙂

@CarstenWickner CarstenWickner added the wontfix This will not be worked on label Jul 15, 2024
@CarstenWickner
Copy link
Member

The support for the Jakarta @AssertTrue/@AssertFalse annotations has been released in v4.36.0.

I'm leaving this issue open for now, as the original topic (Swagger annotations) has not yet been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Development

No branches or pull requests

2 participants