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

Element Template generation: set notEmpty: false explicitly #2675

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.RECORD_COMPONENT})
public @interface TemplateProperty {
public static boolean OPTIONAL_DEFAULT = false;

/** Custom property ID that can be referenced in conditions */
String id() default "";
Expand All @@ -52,7 +53,7 @@
String description() default "";

/** Whether the property should be marked as optional in the element template */
boolean optional() default false;
boolean optional() default OPTIONAL_DEFAULT;

/**
* Overrides the property type. By default, the generator will use the field type to determine the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
package io.camunda.connector.generator.java.processor;

import io.camunda.connector.generator.dsl.PropertyBuilder;
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.util.TemplateGenerationContext;
import java.lang.reflect.Field;

public interface FieldProcessor {

void process(
Field field, PropertyBuilder propertyBuilder, final TemplateGenerationContext context);

static boolean isOptional(Field field) {
var annotation = field.getAnnotation(TemplateProperty.class);
if (annotation == null) {
return TemplateProperty.OPTIONAL_DEFAULT;
}
return annotation.optional();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public void process(
constraintsBuilder.pattern(
new PropertyConstraints.Pattern(pattern.getLeft(), pattern.getRight()));
}

if (pattern != null && !hasNotEmptyConstraint(field) && FieldProcessor.isOptional(field)) {
constraintsBuilder.notEmpty(false);
}
var constraints = constraintsBuilder.build();
if (!isConstraintEmpty(constraints)) {
propertyBuilder.constraints(constraints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void process(
if (annotation == null) {
return;
}
builder.optional(annotation.optional());
builder.optional(FieldProcessor.isOptional(field));

if (!(builder instanceof DropdownPropertyBuilder)) {
if (annotation.feel() == Property.FeelMode.system_default) {
Expand Down Expand Up @@ -182,6 +182,9 @@ private PropertyConstraints buildConstraints(TemplateProperty propertyAnnotation
builder.minLength(constraintsAnnotation.minLength());
}
if (!constraintsAnnotation.pattern().value().isBlank()) {
if (!constraintsAnnotation.notEmpty() && propertyAnnotation.optional()) {
builder.notEmpty(false);
}
builder.pattern(
new PropertyConstraints.Pattern(
constraintsAnnotation.pattern().value(), constraintsAnnotation.pattern().message()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ void validationPresent_notEmpty_objectProperty() {
assertThat(notEmptyProperty.getConstraints().minLength()).isNull();
assertThat(notEmptyProperty.getConstraints().maxLength()).isNull();
}

@Test
void validationPresent_Pattern_optional() {
var template = generator.generate(MyConnectorFunction.MinimallyAnnotated.class).getFirst();
var mayBeEmptyOrRegexValidated = getPropertyById("mayBeEmptyOrRegexValidated", template);
assertThat(mayBeEmptyOrRegexValidated.getConstraints()).isNotNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().notEmpty()).isFalse();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().minLength()).isNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().maxLength()).isNull();
}

@Test
void validationPresent_Pattern_optional_jakarta() {
var template = generator.generate(MyConnectorFunction.MinimallyAnnotated.class).getFirst();
var mayBeEmptyOrRegexValidated =
getPropertyById("mayBeEmptyOrRegexValidatedJakartaStyle", template);
assertThat(mayBeEmptyOrRegexValidated.getConstraints()).isNotNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().notEmpty()).isFalse();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().minLength()).isNull();
assertThat(mayBeEmptyOrRegexValidated.getConstraints().maxLength()).isNull();
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty.DefaultValueType;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyCondition;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyConstraints;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyType;
import io.camunda.connector.generator.java.annotation.TemplateSubType;
import io.camunda.connector.generator.java.example.outbound.MyConnectorInput.AnnotatedSealedType.FirstAnnotatedSubType;
Expand Down Expand Up @@ -99,7 +100,17 @@ public record MyConnectorInput(
@TemplateProperty(
id = "dependsOnBooleanPropertyTrue",
condition = @PropertyCondition(property = "booleanProperty", equals = "true"))
String dependsOnBooleanPropertyTrue) {
String dependsOnBooleanPropertyTrue,
@TemplateProperty(
id = "mayBeEmptyOrRegexValidated",
optional = true,
constraints =
@PropertyConstraints(
pattern = @TemplateProperty.Pattern(value = "xxx", message = "Oh no!")))
String mayBeEmptyOrRegexValidated,
@TemplateProperty(id = "mayBeEmptyOrRegexValidatedJakartaStyle", optional = true)
@Pattern(regexp = "xxx", message = "Oh no!")
String mayBeEmptyOrRegexValidatedJakartaStyle) {

sealed interface NonAnnotatedSealedType permits FirstSubType, NestedSealedType, SecondSubType {

Expand Down