Skip to content

feat(json): propagate field name in FromJsonAny deserialization errors - #467

Open
AAmbuj wants to merge 1 commit into
eclipse-score:mainfrom
AAmbuj:amsh_feat_json_field_contex_in_deserialize_errors
Open

feat(json): propagate field name in FromJsonAny deserialization errors#467
AAmbuj wants to merge 1 commit into
eclipse-score:mainfrom
AAmbuj:amsh_feat_json_field_contex_in_deserialize_errors

Conversation

@AAmbuj

@AAmbuj AAmbuj commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

JsonDeserializeStructImpl now annotates every deserialization failure with the name of the struct field that caused it, stored in the error's UserMessage(). Previously, field identity was lost and only a generic error code was returned.

  • Type mismatch: UserMessage() = failing field name (e.g. "pdu_id")
  • Missing mandatory field: UserMessage() = absent field name
  • Nested struct failure: UserMessage() = outermost containing field name

field_name is a static const char* from struct_visitable, so no heap allocation occurs and no string_view lifetime issue is introduced.

Add three tests covering type mismatch, missing key, and nested struct error propagation cases.

@AAmbuj
AAmbuj deployed to workflow-approval August 12, 2026 11:15 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 12, 2026 11:15 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 12, 2026 11:15 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 12, 2026 11:15 — with GitHub Actions Active
@github-actions github-actions Bot added comp-json Related to score/json component c++ C++ code labels Aug 12, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in BAS - Baselibs FT Aug 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The created documentation from the pull request is available at: docu-html

@4og
4og requested a balanced review from Copilot August 12, 2026 13:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds struct field names to JSON deserialization error messages.

Changes:

  • Annotates type and missing-field errors with field names.
  • Adds tests for direct, missing, and nested-field failures.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
score/json/json_serializer.h Adds field-name error annotation.
score/json/json_serializer_test.cpp Tests field-name propagation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread score/json/json_serializer.h Outdated
Comment on lines +209 to +210
score::cpp::ignore = visitor.error.emplace(
MakeError(static_cast<Error>(*field_content.error()), field_name));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Added Error::WithUserMessage() to error.h which returns a copy preserving the original numeric code and ErrorDomain*, replacing only user_messages_. JsonDeserializeStructImpl now calls field_content.error().WithUserMessage(field_name) instead of MakeError(static_castjson::Error(...))json::Error(...)), so a TypedHash field failure carrying score::hash::ErrorCode::kInvalidParameters propagates with its hash domain intact.

@AAmbuj
AAmbuj force-pushed the amsh_feat_json_field_contex_in_deserialize_errors branch from bb0a5d1 to 17d5a89 Compare August 12, 2026 16:05
@AAmbuj
AAmbuj requested a deployment to workflow-approval August 12, 2026 16:05 — with GitHub Actions Waiting
@AAmbuj
AAmbuj requested a deployment to workflow-approval August 12, 2026 16:05 — with GitHub Actions Waiting
@AAmbuj
AAmbuj requested a deployment to workflow-approval August 12, 2026 16:05 — with GitHub Actions Waiting
@AAmbuj
AAmbuj requested a deployment to workflow-approval August 12, 2026 16:05 — with GitHub Actions Waiting
@github-actions github-actions Bot added the comp-result Related to score/result component label Aug 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview for this pull request is available at:
pr-467: https://eclipse-score.github.io/baselibs/pr-467/

@AAmbuj
AAmbuj force-pushed the amsh_feat_json_field_contex_in_deserialize_errors branch from 17d5a89 to 756d896 Compare August 13, 2026 04:32
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 04:32 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 04:32 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 04:32 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 04:32 — with GitHub Actions Active
JsonDeserializeStructImpl now annotates every deserialization failure
with the name of the struct field that caused it, stored in the error's
UserMessage(). Previously, field identity was lost and only a generic
error code was returned.

- Type mismatch: UserMessage() = failing field name (e.g. "pdu_id")
- Missing mandatory field: UserMessage() = absent field name
- Nested struct failure: UserMessage() = outermost containing field name

field_name is a static const char* from struct_visitable, so no heap
allocation occurs and no string_view lifetime issue is introduced.

Add three tests covering type mismatch, missing key, and nested struct
error propagation cases.
@AAmbuj
AAmbuj force-pushed the amsh_feat_json_field_contex_in_deserialize_errors branch from 756d896 to 7fa9914 Compare August 13, 2026 11:19
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 11:19 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 11:19 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 11:19 — with GitHub Actions Active
@AAmbuj
AAmbuj deployed to workflow-approval August 13, 2026 11:19 — with GitHub Actions Active
// When deserializing into the struct
auto unit{FromJsonAny<TypeToSerialize>(std::move(source))};

// Then the error code is WrongType and the UserMessage names the parent struct field, not the inner field

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we add error for specific error field in nested struct, ie. "nested_bool" , "next_int" etc.
instead of "nested_value" which is again kind of generic error ?

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

Labels

c++ C++ code comp-json Related to score/json component comp-result Related to score/result component

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants