Skip to content

fix(transport) #65: return HTTP 201 Created for newly created objects#195

Merged
lilgallon merged 2 commits intoIZIVIA:mainfrom
rishabhvaish:fix/65-return-201-created-for-new-objects
Mar 27, 2026
Merged

fix(transport) #65: return HTTP 201 Created for newly created objects#195
lilgallon merged 2 commits intoIZIVIA:mainfrom
rishabhvaish:fix/65-return-201-created-for-new-objects

Conversation

@rishabhvaish
Copy link
Copy Markdown
Contributor

The OCPI spec requires that PUT endpoints return HTTP 201 when a new object is created, and 200 when an existing object is updated (https://github.com/ocpi/ocpi/blob/v2.2.1-d2/status_codes.asciidoc). Right now the toolkit always returns 200 regardless.

I went with a coroutine context approach since it fits naturally with how the toolkit already handles ResponseMessageRoutingHeaders and TokenHeader. The idea is:

  1. A new HttpStatusOverride coroutine context element gets installed by TransportServer implementations alongside the existing context elements
  2. Service/repository implementations call signalObjectCreated() when they determine the object is new
  3. respondNullableObject and respondNullableList check for this override and return 201 if set, 200 otherwise

This keeps everything backward-compatible — existing code doesn't change behavior at all. Only services that explicitly opt in by calling signalObjectCreated() will see the 201 response.

I also updated the test Http4kTransportServer to install the context element and added tests covering both the creation (201) and update (200) paths.

Fixes #65

…bjects

Adds an HttpStatusOverride coroutine context element that service
implementations can use to signal object creation via signalObjectCreated().
The respond functions in HttpRequest.kt now check for this override,
returning 201 instead of the default 200 when set.

This is backward-compatible — existing behavior is unchanged unless
the service explicitly calls signalObjectCreated().

Includes tests verifying both the 201 (new object) and 200 (update) cases.
Copy link
Copy Markdown
Member

@lilgallon lilgallon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thanks for your first contribution :)

Any feedback @atschabu / @andacata? If not, we can proceed and merge this

.invertFromRequest(requestMessageRoutingHeaders)

httpRequest to runBlocking(requestMessageRoutingHeaders + responseMessageRoutingHeaders) {
httpRequest to runBlocking(requestMessageRoutingHeaders + responseMessageRoutingHeaders + HttpStatusOverride()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder for me: Add a release note stating that the callback provided to TransportServer.handle() must have HttpStatusOverride in its coroutine context to use signalObjectCreated

@lilgallon
Copy link
Copy Markdown
Member

Build fails, you should run ./gradlew ktlintFormat and amend your commit with linting fixes

Copy link
Copy Markdown
Member

@lilgallon lilgallon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads-up on our internal policy: AI tools are allowed as a development assistant, but using them for automated tasks (Git operations, bulk code generation, etc.) is not.

Could you remove the Claude reference from your commits? Thanks!

Signed-off-by: Rishabh Vaish <rishabhvaish.904@gmail.com>
@rishabhvaish rishabhvaish force-pushed the fix/65-return-201-created-for-new-objects branch from 4a834ad to 501e978 Compare March 25, 2026 07:06
@sonarqubecloud
Copy link
Copy Markdown

@lilgallon lilgallon merged commit 9258d58 into IZIVIA:main Mar 27, 2026
2 checks passed
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.

Object creation returns HTPP 200 instead of an HTTP 201

2 participants