Skip to content

Commit

Permalink
fix: disallow empty 'subject' header
Browse files Browse the repository at this point in the history
Specification clearly states that 'subject' is optional but if present, MUST be non-empty
(spec at https://github.com/cloudevents/spec/blob/v1.0/spec.md#subject)

Signed-off-by: Kristiyan Marinov <[email protected]>
  • Loading branch information
pnt committed Nov 4, 2024
1 parent b54df5c commit a0fda74
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ protected static IllegalStateException createMissingAttributeException(String at
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
}

protected static IllegalStateException createEmptyAttributeException(String attributeName) {
return new IllegalStateException("Attribute '" + attributeName + "' cannot be empty");
}

/**
* Validates the extension name as defined in CloudEvents spec.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public CloudEventV03 build() {
if (type == null) {
throw createMissingAttributeException("type");
}
if (subject != null && subject.isEmpty()) {
throw createEmptyAttributeException(("subject"));
}

CloudEventV03 cloudEvent = new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions);
final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public CloudEvent build() {
if (type == null) {
throw createMissingAttributeException(TYPE);
}
if (subject != null && subject.isEmpty()) {
throw createEmptyAttributeException(("subject"));
}

CloudEvent cloudEvent = new CloudEventV1(id, source, type, datacontenttype, dataschema, subject, time, this.data, this.extensions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,27 @@ void testMissingType() {
).hasMessageContaining("Attribute 'type' cannot be null");
}

@Test
void testMissingSubject() {
CloudEvent actual = CloudEventBuilder
.v03()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.build();

assertThat(actual).isNotNull();
}

@Test
void testEmptySubject() {
assertThatCode(() -> CloudEventBuilder
.v03()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.withSubject("")
.build()
).hasMessageContaining("Attribute 'subject' cannot be empty");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,27 @@ void testValidatorProvider(){
).hasMessageContaining("Expecting sales in namespace extension");
}

@Test
void testMissingSubject() {
CloudEvent actual = CloudEventBuilder
.v1()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.build();

assertThat(actual).isNotNull();
}

@Test
void testEmptySubject() {
assertThatCode(() -> CloudEventBuilder
.v1()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.withSubject("")
.build()
).hasMessageContaining("Attribute 'subject' cannot be empty");
}
}

0 comments on commit a0fda74

Please sign in to comment.