Skip to content

feat(policy-condition): add ViolationType for expression conditions - #64

Open
1azunna wants to merge 1 commit into
DependencyTrack:mainfrom
1azunna:add-policy-condition-violation-type
Open

1azunna wants to merge 1 commit into
DependencyTrack:mainfrom
1azunna:add-policy-condition-violation-type

Conversation

@1azunna

@1azunna 1azunna commented Sep 15, 2026

Copy link
Copy Markdown

Problem

PolicyCondition has no violationType field, so expression conditions cannot be created through this client — DependencyTrack rejects them with HTTP 400.

PolicyConditionResource.maybeValidateExpression (DT 5.0.3):

private void maybeValidateExpression(final PolicyCondition policyCondition) {
    if (policyCondition.getSubject() != PolicyCondition.Subject.EXPRESSION) {
        return;
    }

    if (policyCondition.getViolationType() == null) {
        throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST)
            .entity("Expression conditions must define a violation type").build());
    }
    ...
}

Every other subject supplies its own violation type from the Subject enum, but EXPRESSION is declared EXPRESSION(null) and depends entirely on the explicit field:

public PolicyViolation.Type getViolationType() {
    if (subject != null && subject.violationType != null) {
        return subject.violationType;
    }
    return violationType;
}

Change

  • PolicyCondition.ViolationType, tagged json:"violationType,omitempty"
  • PolicyConditionViolationType with LICENSE, SECURITY, OPERATIONAL — matching PolicyViolation.Type
  • PolicyConditionSubjectExpression, which was also missing from the subject constants

Because of omitempty, conditions using any other subject serialise byte-identically to before, so this is purely additive.

Tests

policy_condition_test.go covers the serialisation contract. Written as unit tests so they run without a container, following util_test.go:

  • violationType is emitted for an expression condition
  • it is omitted when unset, so existing subjects are unaffected
  • it round-trips on unmarshal

go build ./..., go vet ./... and go test -run TestPolicyCondition ./... all pass.

Context

Found while adding expression-condition support to SolarFactories/terraform-provider-dependencytrack, which vendors a fork of this client. I raised it here rather than on that fork because this is the canonical home for the change and the fork is currently identical to main.

@1azunna
1azunna requested a review from nscuro as a code owner September 15, 2026 16:12
@owasp-dt-bot

owasp-dt-bot commented Sep 15, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

DependencyTrack 5.0 rejects a policy condition with subject EXPRESSION
unless it carries a violation type:

    private void maybeValidateExpression(final PolicyCondition policyCondition) {
        if (policyCondition.getSubject() != PolicyCondition.Subject.EXPRESSION) {
            return;
        }
        if (policyCondition.getViolationType() == null) {
            throw new BadRequestException(... "Expression conditions must define a violation type");
        }

Every other subject carries an implicit violation type, declared on the
Subject enum itself, but EXPRESSION is declared as EXPRESSION(null) and so
depends entirely on the explicit field. Without it on the struct there is no
way to create an expression condition through this client.

Adds PolicyConditionViolationType (LICENSE, SECURITY, OPERATIONAL, matching
PolicyViolation.Type) and the missing PolicyConditionSubjectExpression
constant. The field is omitempty so conditions using any other subject
serialise exactly as before.

Signed-off-by: 1azunna <26540897+1azunna@users.noreply.github.com>
@nscuro

nscuro commented Sep 18, 2026

Copy link
Copy Markdown
Member

Thanks @1azunna! I am wondering whether we should bump the major version of this library before we start introducing v5-specific stuff. It'd be a pain to have the client work with v4 and v5 going forward.

Thinking to cut a v1 release (since we're currently at v0 still) of the current state, and then merging your PRs into what will then become v2. WDYT?

@1azunna

1azunna commented Sep 18, 2026

Copy link
Copy Markdown
Author

Thanks @1azunna! I am wondering whether we should bump the major version of this library before we start introducing v5-specific stuff. It'd be a pain to have the client work with v4 and v5 going forward.

Thinking to cut a v1 release (since we're currently at v0 still) of the current state, and then merging your PRs into what will then become v2. WDYT?

I think v4 and v5 compatible features can go in the v1 release and the v2 release can be strictly v5 features.

@1azunna 1azunna closed this Sep 18, 2026
@1azunna 1azunna reopened this Sep 18, 2026
@nscuro nscuro mentioned this pull request Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants