Skip to content

[BUG] PUT /api/channels/{id} without override always fails for channels that have no lastModified #590

Open

Description

@iacsha

Describe the bug

Updating an existing channel through the REST API with override=false (the default) returns false and doesn't save, even when nobody else has touched the channel. It happens every time for channels that have never been saved from the Administrator, for example channels created or imported through the API, or by tools such as mirthsync. The only way to save them is override=true, which also turns off the concurrent-edit check.

From the code on main (and the same in 4.5.2), DefaultChannelController.updateChannel decides this at:

if ((currentRevision > 0) && !override && (dateStartEdit.before(lastModifiedDate))) {
    return false;
}
  • ChannelServlet.updateChannel sets dateStartEdit to the time of the request when the caller doesn't send startEdit.
  • lastModifiedDate comes from ChannelMetadata.getLastModified(), which returns Calendar.getInstance() when no lastModified has been recorded:
public Calendar getLastModified() {
    if (lastModified == null) {
        lastModified = Calendar.getInstance();
    }
    return lastModified;
}

So for a channel with no recorded modification time, lastModified is always a few milliseconds after startEdit, and the update is always refused.

Two related points:

  • The API docs for override say "If true, the channel will be updated even if a different revision exists on the server", but the check never compares revisions. After a channel does have a lastModified, an unforced PUT with a stale <revision> succeeds and overwrites the newer version.
  • The startEdit example in the docs is 1985-10-26T09:00:00.000-0700, but the servlet parses with yyyy-MM-dd'T'HH:mm:ssZ. As far as I can tell, the documented format, with milliseconds, fails to parse, gets logged as "Error parsing start edit date", and falls back to "now".

To Reproduce

Stock OIE 4.5.2 (official Docker image, Derby):

  1. Create a channel through the API: POST /api/channels.
  2. GET /api/channels/{id}, change only the description, then PUT /api/channels/{id}?override=false with that XML.
  3. The response is false, and the change isn't saved.
  4. The same PUT with override=true saves, and so does the same PUT with startEdit set to a future time.

Expected behavior

An update to a channel that nobody else has changed since the caller read it is saved without override=true. A caller holding an older revision is refused, as the docs describe.

Actual behavior

Every unforced update of a channel without a recorded lastModified is refused. For channels that do have one, the decision is made on timestamps only, so a stale revision is not caught.

Environment

  • OS: Linux (Docker)
  • Java: the official image's bundled JRE
  • Version: OIE 4.5.2. The code is unchanged on main.

Workaround(s)

Send override=true (mirthsync's -f), or save the channel once from the Administrator so it gets a lastModified.

Additional context

Possible fixes, smallest first:

  1. In updateChannel, treat a missing lastModified as "no conflict" instead of "modified now", for example by reading the stored field without the lazy default.
  2. Compare channel.getRevision() against the stored revision when override is false, which is what the docs describe and what the channel group update already does (group.getRevision().equals(matchingGroup.getRevision())).

I'm happy to open a PR for either, or both, if one direction is preferred. Found while testing mirthsync, where every unforced push of an API-created channel fails with "Unable to save the channel".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions