sdk-javascript icon indicating copy to clipboard operation
sdk-javascript copied to clipboard

SHOULD throw if subject is empty string (parsed from structured content mode)?

Open c-pius opened this issue 4 years ago • 2 comments

Describe the Bug Spec states that the subject MUST be a non-empty string. In structured content mode HTTP.toEvent adds an empty subject string as undefined to the event and calling validate on the resulting event does therefore NOT throw.

In binary content mode, HTTP.toEvent adds the empty subject as subject: '' and a subsequent call on validate throws subject should NOT be shorter than 1 characters. This would be the correct behavior I assume.

Steps to Reproduce Use the following snippet:

const event = HTTP.toEvent({
  headers: {
    'Content-Type': 'application/cloudevents+json'
  },
  body: {
    id: '0815',
    specversion: '1.0',
    type: 'my.event.type',
    source: 'my.event.source',
    subject: ''
  }
})

// event has subject: undefined

event.validate() // does not throw

Expected Behavior event should have subject: '' and event.validate() should throw.

Additional context

property is the empty string here and therefore the parser is not executed https://github.com/cloudevents/sdk-javascript/blob/6be3b2751401e529fce6ccf8a406a057472c10a9/src/message/http/index.ts#L210

In general not sure if this should be an error on HTTP.toEvent already or only at validate

c-pius avatar Feb 10 '21 15:02 c-pius

event should have subject: '' and event.validate() should throw.

Hmm, since subject is an optional attribute, it seems odd to throw if it's empty - even if the spec states that it should be a non-empty string. I would argue that the binary parser is wrong, and that it should not throw when receiving a header such as ce-subject: "".

lance avatar Mar 05 '21 20:03 lance

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Apr 05 '21 00:04 github-actions[bot]