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

Bean validation's @Size does not work with collections #348

Closed
magx2 opened this issue Mar 13, 2019 · 7 comments
Closed

Bean validation's @Size does not work with collections #348

magx2 opened this issue Mar 13, 2019 · 7 comments
Labels
Milestone

Comments

@magx2
Copy link

magx2 commented Mar 13, 2019

I see that you do not support annotation javax.validation.constraints.Size. Can you support it?

From java doc:

The annotated element size must be between the specified boundaries (included).
Supported types are:
CharSequence (length of character sequence is evaluated)
Collection (collection size is evaluated)
Map (map size is evaluated)
Array (array length is evaluated)
null elements are considered valid.

@fmbenhassine
Copy link
Member

@Size is supported and here is an example: Annotated bean and test.

I see that you do not support annotation javax.validation.constraints.Size

Where do you see that?

@magx2
Copy link
Author

magx2 commented Mar 14, 2019

Hmm I used it on List and it didn't work.

@fmbenhassine
Copy link
Member

fmbenhassine commented Mar 14, 2019

It looks like only the String type is supported. Here is a failing test with v3.9.0:

import java.util.List;

import javax.validation.constraints.Size;

import io.github.benas.randombeans.EnhancedRandomBuilder;
import io.github.benas.randombeans.api.EnhancedRandom;
import org.junit.Assert;
import org.junit.Test;

public class Issue348 {

	@Test
	public void testSizeOnList() {
		// given
		EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
				.build();

		// when
		Person person = enhancedRandom.nextObject(Person.class);

		// then
		Assert.assertNotNull(person);
		int size = person.getNames().size();
		Assert.assertTrue(size >= 2 && size <= 5);
	}

	static class Person {
		@Size(min = 2, max = 5)
		private List<String> names;

		public Person() {
		}

		public List<String> getNames() {
			return names;
		}

		public void setNames(List<String> names) {
			this.names = names;
		}
	}
}

We need to add support for all types as documented in the annotation (Collection, Map and Array) in addition to Strings.

Thank you for reporting this issue!

@fmbenhassine fmbenhassine added this to the 4.0.0 milestone Mar 14, 2019
@magx2
Copy link
Author

magx2 commented Mar 14, 2019

Maybe use Collection interface instead of List?

@fmbenhassine
Copy link
Member

Yes as I said here:

We need to add support for all types as documented in the annotation (Collection, Map and Array) in addition to Strings.

@fmbenhassine fmbenhassine modified the milestones: 4.0.0.RC1, 4.0.0.RC2 Mar 14, 2019
@fmbenhassine fmbenhassine changed the title Support Size Bean validation's @Size does not work with collections Mar 16, 2019
fmbenhassine added a commit that referenced this issue Mar 17, 2019
@fmbenhassine
Copy link
Member

@magx2 A fix has been deployed in version 4.0.0.RC2-SNAPSHOT. Can you please give it a try?

If you don't know how to use a snapshot version, please refer to the wiki here.

As you might have noticed, the project has been renamed and you would need to adjust a couple of things (see migration guide).

Looking forward for your feedback. Thank you upfront!

@magx2
Copy link
Author

magx2 commented Mar 18, 2019

It's working! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants