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

@Size annotation on List<String> causes incorrect openapi spec to be generated. #2337

Closed
caspianb opened this issue Aug 8, 2023 · 3 comments
Labels
invalid This doesn't seem right

Comments

@caspianb
Copy link

caspianb commented Aug 8, 2023

Using a simple DTO can reproduce this issue:

static class RequestDto {
    @Size(min = 1, max = 5)
    @Schema(description = "List of values.")
    private List<String> values;
}

The @Size validation is only (correctly) applied to the list size. However, the api spec itself is showing this:
image

The generated API spec is stating that:

  • the list must be between 1 and 5 elements (correct);
  • the length of the string of each element must be between 1 and 5 characters (incorrect)

You can also see that the description is also being duplicated from the array down to the element as well. I am uncertain if this is intentional and or desirable so not sure if that should also be considered a bug as well.

@bnasslahsen
Copy link
Contributor

@caspianb,

This looks to be correct.
Anyway, you can ask the swagger-core project for more explanation, as this is purely handled by swagger-core-jakarta

public class DemoApplication {

	record TestDTO(@Size(min = 1, max = 5)
						  @Schema(description = "List of values.")
						  List<String> values) {

	}
	
	public static void main(String[] args) {
		ResolvedSchema resolvedSchema = ModelConverters.getInstance()
				.resolveAsResolvedSchema(
						new AnnotatedType(TestDTO.class).resolveAsRef(false));
		resolvedSchema.schema.getMinLength();
		Assert.isTrue(((ArraySchema) resolvedSchema.schema.getProperties().get("values")).getMaxItems().equals(5));
		Assert.isTrue(((ArraySchema) resolvedSchema.schema.getProperties().get("values")).getItems().getMaxLength().equals(5));
	}
	
}

@bnasslahsen bnasslahsen added the invalid This doesn't seem right label Aug 8, 2023
@caspianb
Copy link
Author

caspianb commented Aug 8, 2023

Thanks for the quick response! -- I'll log an issue at swagger-core.

This looks to be correct.

Can you explain this to me, though? The item maxLength relates to the String itself. The @Size annotation only applies to the List, though. The String can still be any length (and that's how the spring-boot validations work). It seems like the openApi spec is saying the String must be between 1 and 5 characters -- which is not correct?

(Also note this just started occurring after the 2.2.0 update).

Is there something I'm misunderstanding here?

@bnasslahsen
Copy link
Contributor

@caspianb,

You have already been answered.
No more answer will be added in this ticket.

@springdoc springdoc locked as too heated and limited conversation to collaborators Aug 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants