Add a param converter and DTO for XB data model

Created on 26 November 2024, 9 months ago

Overview

Methods such as those in ClientServerConversionTrait have logic around converting incoming request bodies into Typed data objects but also mix in some validation results

Proposed resolution

Move this to a data transfer object that is built from a routing param converter
Make the param converter utilise the serializer component?
Then controllers can just typehint on the DTO as a param and the logic around routing and validation can be lifted out of the controller.
The data transfer (value) object can have ::isValid ::getViolations and ::getModel/::getLayout/::getTree as required

User interface changes

šŸ“Œ Task
Status

Active

Version

0.0

Component

Data model

Created by

šŸ‡¦šŸ‡ŗAustralia larowlan šŸ‡¦šŸ‡ŗšŸ.au GMT+10

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @larowlan
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    Methods such as those in ClientServerConversionTrait have logic around converting incoming request bodies into Typed data objects but also mix in some validation results

    All of that is a stop-gap solution, the top priority should be resolving šŸ› Some components cannot be used in XB because UI prevents SDC props being named `name` Active , to evolve the client-side data model to support all known (current and ideally also future) needs.

    @larowlan Do you agree with resolving that issue first? I've started the conversations for that in the week before my paternity leave, to set the stage, but it has unfortunately not been followed through. šŸ˜ž

  • šŸ‡¦šŸ‡ŗAustralia larowlan šŸ‡¦šŸ‡ŗšŸ.au GMT+10

    Seems reasonable to me

  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    The intent/scope of this issue has never been super clear to me.

    Related discussion:

    Wim, Dec 19, 2024:

    šŸ¤” This is a client-side/UI model. It feels wrong/off to put this logic in a pure server-side representation? šŸ™ˆ

    Wouldn't it be better to create a ClientSideModel typed object on the server side that can contain all the relevant transformation methods?

    Lee, Dec 19, 2024:

    Yes, I've been wanting to do this for a long time - see https://www.drupal.org/project/experience_builder/issues/3489772 šŸ“Œ Add a param converter and DTO for XB data model Active and !137

    Very very keen.

    On pure symfony API projects I have, we do this in request events so that controllers can type-hint the object and have zero logic of 'how do I take this request body and make something from it'. Realllllly keen to do that here. Thin controllers++

    — https://git.drupalcode.org/project/experience_builder/-/merge_requests/4..., for šŸ“Œ Implement auto-save of the page template config entity Active

    That sounds amazing, so if that's the intent for this issue, then hell yes! šŸ™šŸ˜„

  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    While working on šŸ“Œ Improve or remove ComponentSourceInterface::getClientSideInfo() Active , it became clear that a subset of what's described in this issue's summary appeared there. See https://git.drupalcode.org/issue/experience_builder-3484678/-/tree/34846..., described in #3484678-20: Improve or remove ComponentSourceInterface::getClientSideInfo() → , @larowlan šŸ¤“

  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    šŸ“Œ Improve or remove ComponentSourceInterface::getClientSideInfo() Active just landed. Even if this takes us in a different direction (because that was just an incremental improvement, not meant to be final), then that work should make this issue much simpler 😊

  • Status changed to Postponed 6 months ago
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    šŸ“Œ Simplify ClientServerConversionTrait::clientLayoutToServerTree() Active is in, which is another small step towards making this (or something like it) feasible.

  • šŸ‡ŗšŸ‡øUnited States tedbow Ithaca, NY, USA

    I just noticed the comments at the end of \Drupal\experience_builder\Controller\ClientServerConversionTrait::convertClientToServer are wrong

    // Update the entity, validate and save.
        // Note: constructing ComponentTreeStructure from `layout` and
        // ComponentInputs from `model` also included validation. But that
        // included only structural validation, not semantical validation.
        // @see \Drupal\experience_builder\Plugin\Field\FieldType\ComponentTreeItem
        // @see \Drupal\experience_builder\Plugin\Validation\Constraint\ValidComponentTreeConstraintValidator
        // @see \Drupal\experience_builder\Plugin\Validation\Constraint\ComponentTreeMeetsRequirementsConstraintValidator
        return [
          'tree' => json_encode($tree, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR),
          'inputs' => json_encode($inputs, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT),
        ];
    

    re "Update the entity, validate and save." we don't do any of this here any more, probably did from where ever this was refactored from .

    Don't know if I should make a new issue or fix or this is going away in the current issue

  • šŸ‡¦šŸ‡ŗAustralia larowlan šŸ‡¦šŸ‡ŗšŸ.au GMT+10

    I'm personally still in favour of this because

    a) I want to see \Drupal\experience_builder\Controller\ClientServerConversionTrait gone. It's use in Patterns, PageRegions etc feels wrong
    b) I want to see the static methods on that trait returned to non static methods on the service.
    c) I don't want the controllers to have to work with the raw data, I want the request object to have upcast objects automatically

  • šŸ‡«šŸ‡®Finland lauriii Finland
Production build 0.71.5 2024