Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STNG-184 Improve all DSP and related classes #198

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

jkosternl
Copy link
Collaborator

  • Use Jackson ObjectMapper functionality if possible

* Use Jackson ObjectMapper functionality if possible
@jkosternl jkosternl self-assigned this Oct 15, 2024
@jkosternl jkosternl marked this pull request as draft October 15, 2024 14:14
@jkosternl jkosternl marked this pull request as ready for review October 16, 2024 08:11
@douradofrazer
Copy link

@CodiumAI-Agent /describe
--pr_description.publish_description_as_comment=true
--pr_description.generate_ai_title=true

@CodiumAI-Agent
Copy link

Title

Enhance JSON Handling and Refactor Placeholder Replacements Across Modules


User description

  • Use Jackson ObjectMapper functionality if possible

PR Type

enhancement, bug_fix, tests


Description

  • Refactored multiple classes to simplify JSON conversion using OBJECT_MAPPER.valueToTree and OBJECT_MAPPER.convertValue.
  • Added @JsonInclude annotation to several classes to exclude null values from JSON serialization.
  • Improved handling of null values in JsonToolkit to ensure they are replaced with "null" string.
  • Added unit tests for DynamicScenarioParameters to verify JSON conversion methods.
  • Refactored placeholder replacements in BookingShipper and EblIssuanceCarrier using a HashMap for cleaner code.

Changes walkthrough 📝

Relevant files
Enhancement
9 files
BookingShipper.java
Refactor placeholder replacements in BookingShipper           

booking/src/main/java/org/dcsa/conformance/standards/booking/party/BookingShipper.java

  • Refactored placeholder replacements using a HashMap.
  • Simplified the call to JsonToolkit.templateFileToJsonNode.
  • +22/-25 
    CarrierScenarioParameters.java
    Simplify JSON conversion in CarrierScenarioParameters       

    booking/src/main/java/org/dcsa/conformance/standards/booking/party/CarrierScenarioParameters.java

  • Simplified JSON conversion using OBJECT_MAPPER.valueToTree.
  • Updated fromJson method to use OBJECT_MAPPER.convertValue.
  • +13/-36 
    DynamicScenarioParameters.java
    Enhance JSON handling in DynamicScenarioParameters             

    booking/src/main/java/org/dcsa/conformance/standards/booking/party/DynamicScenarioParameters.java

  • Added @JsonInclude annotation to exclude nulls.
  • Simplified JSON conversion methods.
  • +7/-49   
    EblIssuanceCarrier.java
    Refactor placeholder replacements in EblIssuanceCarrier   

    ebl-issuance/src/main/java/org/dcsa/conformance/standards/eblissuance/party/EblIssuanceCarrier.java

  • Refactored placeholder replacements using a HashMap.
  • Simplified the call to JsonToolkit.templateFileToJsonNode.
  • +16/-17 
    CarrierScenarioParameters.java
    Enhance JSON handling in CarrierScenarioParameters             

    ebl/src/main/java/org/dcsa/conformance/standards/ebl/party/CarrierScenarioParameters.java

  • Added @JsonInclude annotation to exclude nulls.
  • Simplified JSON conversion methods.
  • +6/-31   
    DynamicScenarioParameters.java
    Enhance JSON handling in DynamicScenarioParameters             

    pint/src/main/java/org/dcsa/conformance/standards/eblinterop/models/DynamicScenarioParameters.java

  • Added @JsonInclude annotation to exclude nulls.
  • Simplified JSON conversion methods.
  • +12/-29 
    ReceiverScenarioParameters.java
    Enhance JSON handling in ReceiverScenarioParameters           

    pint/src/main/java/org/dcsa/conformance/standards/eblinterop/models/ReceiverScenarioParameters.java

  • Added @JsonInclude annotation to exclude nulls.
  • Simplified JSON conversion methods.
  • +6/-13   
    SenderScenarioParameters.java
    Simplify JSON conversion in SenderScenarioParameters         

    pint/src/main/java/org/dcsa/conformance/standards/eblinterop/models/SenderScenarioParameters.java

  • Simplified JSON conversion using OBJECT_MAPPER.valueToTree.
  • Updated fromJson method to use OBJECT_MAPPER.convertValue.
  • +9/-19   
    SandboxWaiting.java
    Simplify JSON conversion in SandboxWaiting                             

    sandbox/src/main/java/org/dcsa/conformance/sandbox/SandboxWaiting.java

  • Simplified JSON conversion using OBJECT_MAPPER.valueToTree.
  • Updated fromJson method to use OBJECT_MAPPER.convertValue.
  • +8/-13   
    Tests
    1 files
    DynamicScenarioParametersTest.java
    Add unit tests for DynamicScenarioParameters                         

    booking/src/test/java/org/dcsa/conformance/standards/booking/party/DynamicScenarioParametersTest.java

  • Added unit tests for DynamicScenarioParameters.
  • Tested toJson and fromJsonNode methods.
  • +25/-0   
    Bug fix
    1 files
    JsonToolkit.java
    Improve null value handling in JsonToolkit                             

    core/src/main/java/org/dcsa/conformance/core/toolkit/JsonToolkit.java

  • Improved handling of null values in template replacements.
  • Ensured nulls are replaced with "null" string.
  • +5/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    return JsonToolkit.templateFileToJsonNode(
    "/standards/booking/messages/"+ scenarioType.bookingTemplate(apiVersion),
    Map.ofEntries(
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Interesting, what was wrong about this way of writing it?

    Copy link
    Collaborator Author

    Choose a reason for hiding this comment

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

    Actually, nothing is wrong with the existing way of writing that. But since I changed the mapper, some values could be null, which is not allowed if you use Map.entry(). So, I favored to use the generic way of mapping objects and I needed to change this code here, to prevent failures.

    Next to that, I changed JsonToolkit.templateFileToJsonNode method, to get the same replacing behavior as before, because developers used cspNode.required("commodityType1").asText(), way to create the Java object, which causes the String objects to get filled with "null" text.
    Actually, I am now in doubt if I really need both changes. Will check if the null-check is really needed later (before merging).

    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Personally I'd favor filtering the nulls in the JsonToolkit method, so everyone can use the fluent API without worrying about them. As for values appearing as the string "null", I'd view it as an unintended side effect that happens to work rather than as the desired behavior 🙂

    replacements.forEach((key, value) -> jsonString.set(jsonString.get().replaceAll(key, value)));
    replacements.forEach(
    (key, value) -> {
    if (value == null) value = "null"; // Null values are replaced with "null"
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Interesting, what's wrong with a "proper" null (or a null or missing node)?

    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.

    5 participants