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

Add non-null constraints when in a Jspecify @NullMarked module/package #257

Merged
merged 5 commits into from
Aug 31, 2024

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Aug 30, 2024

  • When a Validation class is in a @NullMarked module/package/type, all field constraints are augmented with a non-null check unless @Nullable is specified

Given a class like:

@Valid
@NullMarked
public record JSpecifyNotNull(String basic, String withMax, @Nullable String withCustom) {}

the following is generated:

@Generated("avaje-validator-generator")
public final class JSpecifyNotNullValidationAdapter implements ValidationAdapter<JSpecifyNotNull> {

  private final ValidationAdapter<String> basicValidationAdapter;
  private final ValidationAdapter<String> withMaxValidationAdapter;
  private final ValidationAdapter<JSpecifyNotNull> jspecifyNotNullValidationAdapter;

  public JSpecifyNotNullValidationAdapter(ValidationContext ctx) {
    this.basicValidationAdapter = 
        ctx.<String>adapter(NonNull.class, Map.of("message","{avaje.NotNull.message}"));

    this.withMaxValidationAdapter = 
        ctx.<String>adapter(NonNull.class, Map.of("message","{avaje.NotNull.message}"));

    this.jspecifyNotNullValidationAdapter = 
        ctx.<JSpecifyNotNull>adapter(NonNull.class, Map.of("message","{avaje.NotNull.message}"));

  }

  @Override
  public boolean validate(JSpecifyNotNull value, ValidationRequest request, String field) {
    if (field != null) {
      request.pushPath(field);
    }
    var _$basic = value.basic();
    basicValidationAdapter.validate(_$basic, request, "basic");

    var _$withMax = value.withMax();
    withMaxValidationAdapter.validate(_$withMax, request, "withMax");

    if (!request.hasViolations()) {
      jspecifyNotNullValidationAdapter.validate(value, request, field);
    }


    if (field != null) {
      request.popPath();
    }
    return true;
  }
}

@SentryMan SentryMan added this to the 2.2 milestone Aug 30, 2024
@SentryMan SentryMan requested a review from rbygrave August 30, 2024 22:50
@SentryMan SentryMan self-assigned this Aug 30, 2024
@SentryMan SentryMan added the enhancement New feature or request label Aug 30, 2024
@SentryMan SentryMan changed the title Add non-null constraints using jspecify Add non-null constraints when in a Jspecify @NullMarked module/package Aug 31, 2024
@rbygrave rbygrave merged commit e495eb5 into avaje:main Aug 31, 2024
6 checks passed
@rbygrave
Copy link
Contributor

Yeah, very nice!!

@SentryMan SentryMan deleted the jspecify-nullmark branch August 31, 2024 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants