Skip to content

Commit

Permalink
Fix for random sizes for collections in complex objects (#20)
Browse files Browse the repository at this point in the history
* Fix random sizes for collections

* Increase minor version

* Fix wrong naming assignemnt
  • Loading branch information
apenlor authored Nov 6, 2023
1 parent a48a930 commit c275752
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>pact-annotation-processor</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<name>PactDslBuilder - Annotation Processor</name>
<description>Pact DSL Builder annotation processor.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
Expand Down Expand Up @@ -244,15 +245,22 @@ private String cleanType(final Element element) {
private FieldValidations extractValidations(final Element element) {
final var validationBuilder = FieldValidations.builder();

int minValue = 0;
int maxValue = 0;
final var type = element.asType();
if (CollectionUtils.isNotEmpty(type.getAnnotationMirrors())) {
for (var annotation : type.getAnnotationMirrors()) {
if (annotation.getAnnotationType().toString().toUpperCase().endsWith("MAX")) {
validationBuilder.max(((Long) Objects.requireNonNull(getAnnotationValue(annotation, "value")).getValue()).intValue());
maxValue = ((Long) Objects.requireNonNull(getAnnotationValue(annotation, "value")).getValue()).intValue();
validationBuilder.max(maxValue);
} else {
validationBuilder.min(((Long) Objects.requireNonNull(getAnnotationValue(annotation, "value")).getValue()).intValue());
minValue = ((Long) Objects.requireNonNull(getAnnotationValue(annotation, "value")).getValue()).intValue();
validationBuilder.min(minValue);
}
}
//For random size calculation: defaults to +10 elements max if not defined.
maxValue = (maxValue == 0) ? (minValue + 10) : maxValue;
validationBuilder.randomSize(new Random().nextInt(maxValue - minValue + 1) + minValue);
}
return validationBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class FieldValidations {

Integer max;

Integer randomSize;

String regex;

}
11 changes: 8 additions & 3 deletions src/main/resources/templates/templateDslBuilder.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
<#macro writeBuilderComplexField complexField>
<#if complexField.complexType.name() == "COLLECTION">
<#if complexField.fieldValidations??>
<#if complexField.fieldValidations.randomSize??>
<#assign randomSize = "${complexField.fieldValidations.randomSize}"/>
<#else >
<#assign randomSize = ""/>
</#if>
<#if complexField.fieldValidations.max??>
<#assign max = "${complexField.fieldValidations.max}"/>
<#else >
Expand All @@ -165,19 +170,19 @@
if (${complexField.name}.isEmpty()) {
pactDslJsonBody.minMaxArrayLike("${complexField.name}", ${min}, ${max});
} else {
pactDslJsonBody.minMaxArrayLike("${complexField.name}", ${min}, ${max}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${min});
pactDslJsonBody.minMaxArrayLike("${complexField.name}", ${min}, ${max}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${randomSize});
}
<#elseif min?has_content>
if (${complexField.name}.isEmpty()) {
pactDslJsonBody.minArrayLike("${complexField.name}", ${min});
} else {
pactDslJsonBody.minArrayLike("${complexField.name}", ${min}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${min});
pactDslJsonBody.minArrayLike("${complexField.name}", ${min}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${randomSize});
}
<#elseif max?has_content>
if (${complexField.name}.isEmpty()) {
pactDslJsonBody.maxArrayLike("${complexField.name}", ${max});
} else {
pactDslJsonBody.maxArrayLike("${complexField.name}", ${max}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${max});
pactDslJsonBody.maxArrayLike("${complexField.name}", ${max}, <@writeBuilderLambdaField fieldList=complexField.fields/>, ${randomSize});
}
<#else >
pactDslJsonBody.eachLike("${complexField.name}", <@writeBuilderLambdaField fieldList=complexField.fields/>, 1);
Expand Down

0 comments on commit c275752

Please sign in to comment.