NextGen APIs

Make a field optional

This customization case, such as that of adding a step before and/or after the default logic, is frequent and is worth seeing in detail.

Although it may seem like a trivial customization, it is a strong customization that necessarily requires the creation of a new API that can manage an input DTO that has custom requirements, therefore different from those provided in the default DTO.

Let's see all the necessary steps.

First of all, the Request's internal flow is shown on the Customizations page, do you remember the structure? Controller <-> Service <-> Component

  1. New Custom Controller (CustomAssetsController)

    1. New custom API (/integration/assets/r1/asset-custom)

      1. New custom input DTO (CustomAssetUpsertRequestDTO)

        1. New custom Service interface injected (CustomAssetService

Screenshot 2025-02-18 alle 11.54.44.png

💡 Look at return, where the Mono chain, returned in output by the upsert method of the new custom service, is blocked.

  1. New custom Service Implementation (CustomAssetServiceImpl)

    1. which implements the New custom Service interface (CustomAssetService)

      1. New custom Component interface injected (CustomAssetUpsert)

Screenshot 2025-02-18 alle 11.55.28.png

💡 The service layer is inserted into the reactive flow by calling the upsert method of the new custom component interface and returning the Mono chain, processed in the component, to the controller.

  1. New custom Component Implementation (CustomAssetUpsertImpl)

    1. which implements the New custom Component interface (CustomAssetUpsert)

      1. Legacy default component’s interface is injected (AssetUpsert)

      2. New Custom Mapper is injected (CustomAssetUpsertMapper)

Screenshot 2025-02-18 alle 11.56.26.png

💡 Component’s implementation steps:

  • Calculate mandatory field value

    • It could be a constant value (like the example above) or a calculated value, perhaps using a specific component to demand the execution of the calculating logic

  • Map custom DTO on default DTO

    • The new custom mapper allows you to automatically map all the fields of the custom DTO to the default DTO

  • Set mandatory field with calculated value

    • The customization was made to be able to manage a request DTO with a field which, although mandatory in the default DTO, is managed as optional in the custom one: in order to be able to move from the legacy implementation logics without running into an input validation error, the mandatory field in the legacy code must necessarily be set 'by hand', following a calculation or a setting by constant value.

  • Call default api algorithm with mandatory field populated

    • Finally, after having built the default DTO starting from the custom one with all the necessary settings, the default component (AssetUpsert) is recalled to use the legacy logic already implemented (considering that the legacy implementation is already managed in reactive mode it’s not necessary to ‘wrap output’ in a new Mono)