Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Command deserialization #1972

@pintomau

Description

@pintomau

Hi.

On our services, we're trying to keep API compatibility with Commercetools while enriching it where we need to.

For example, if some client application sends us a cart creation request json, we can deserialize it using the CartDraft model and enrich the request specific to our platform:

  public Cart save(final CartDraft cartDraft) {

    final CustomFieldsDraft originalCustom = cartDraft.getCustom();

    final CustomFieldsDraft enrichedCustom =
        CustomFieldsDraftBuilder
            // force CE custom type
            .ofTypeKey(CE_CUSTOM_TYPE_KEY)
            // carry over any custom fields, if they exist
            .fields(
                originalCustom == null || originalCustom.getFields() == null
                    ? new HashMap<>()
                    : new HashMap<>(originalCustom.getFields()))
            // add sourceId field
            .addObject(CART_SOURCE_ID_FIELD_NAME, ctx.getSourceId())
            .build();

    final CartDraftDsl enrichedCart = CartDraftDsl.of(cartDraft).withCustom(enrichedCustom);
    return executor.executeBlocking(CartCreateCommand.of(enrichedCart));
  }

Now we wanted to go further here and also enrich/control Update Commands, for example limit what actions a client can execute, add commands on top of what the client sends us...

We've also considered using HTTP API extensions for this, but since it lacks context (which actions were executed and what the cart looked like before it was changed), we can't do the full scope of interactions/validations we wanted to. Also, it saves another roundtrip.

What we want to achieve would look something like this:

  @PostMapping("{cartId}")
  public ResponseEntity<Cart> updateCart(
      @PathVariable final UUID cartId, @RequestBody final CartUpdateCommand updateCommand) {

    // intercept and manipulate the update command

    // forward the updated command to Commercetools

    // be happy :)
    return null;
  }

We tried shadowing CartUpdateCommand to add @JsonDeserialize(as = CartUpdateCommandImpl.class), but noticed that CartUpdateCommand expects a Versioned<Cart> type that isn't available.

We also tried building an object much like UpdateCommandBody<T>, but the UpdateAction<T> lacks any deserialization context to be able to deserialize into its subtypes.

Wondering if you have any suggestion so we could avoid creating all actions ourselves and then translate them into the Commercetools' model, or maybe we've missed some way to do this in the SDK.

Thanks.

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