Skip to content

Fix localized string converter for reading plugin configuration#694

Closed
ze-dom wants to merge 2 commits intoMUnique:masterfrom
ze-dom:fix_localized_string_converter_read_plugin_configration
Closed

Fix localized string converter for reading plugin configuration#694
ze-dom wants to merge 2 commits intoMUnique:masterfrom
ze-dom:fix_localized_string_converter_read_plugin_configration

Conversation

@ze-dom
Copy link
Contributor

@ze-dom ze-dom commented Jan 29, 2026

After the multilanguage support PR, I was having exceptions when starting up the DB. It turned out LocalizedStrings within PlugInConfiguration.CustomConfiguration couldn't be read by the LocalizedStringJsonConverter because those are kept as objects within the JSON data:
Screenshot from 2026-01-29 11-40-46

The LocalizedStrings were also not showing up on the AdminPanel's edit modal.

@gemini-code-assist
Copy link

Summary of Changes

Hello @ze-dom, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug where localized strings within plugin configurations were not being deserialized correctly from JSON, leading to application startup exceptions and display issues in the AdminPanel. The changes ensure that LocalizedString objects are properly handled during JSON serialization and deserialization, and that UI components gracefully manage nullable localized string values, improving overall stability and user experience for multi-language support.

Highlights

  • JSON Deserialization Fix: The LocalizedStringJsonConverter has been updated to correctly deserialize LocalizedString objects when they are embedded as JSON objects (e.g., {"Key": "Value"}) within plugin configurations, resolving startup exceptions.
  • Preventing Redundant Serialization: The ValueInNeutralLanguage and ValueInNeutralLanguageAsSpan properties in LocalizedString are now marked with [JsonIgnore] to prevent them from being serialized as separate properties, which could lead to incorrect JSON structures.
  • Nullable LocalizedString Handling in UI: UI components (LocalizedStringFieldBuilder and LocalizedStringField.razor) have been modified to gracefully handle nullable LocalizedString values, preventing potential null reference exceptions and ensuring proper display in the AdminPanel.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue with deserializing LocalizedString from plugin configurations by updating the LocalizedStringJsonConverter to handle object formats. It also adds [JsonIgnore] attributes to derived properties and modifies UI components for nullable LocalizedString support. My review highlights two areas for improvement: the JSON converter's parsing logic could be more robust, and the LocalizedStringFieldBuilder's type checking may be too restrictive. The suggestions provided aim to enhance the resilience and correctness of these components.

Comment on lines +28 to +38
if (reader.TokenType == JsonTokenType.StartObject)
{
reader.Read();
if (reader.TokenType == JsonTokenType.PropertyName)
{
reader.Read();
var value = reader.GetString();
reader.Read(); // consume the end of object
return new LocalizedString(value);
}
}

Choose a reason for hiding this comment

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

medium

The current implementation for reading a LocalizedString from a JSON object is fragile. It assumes the object contains exactly one property and doesn't verify its name. This could lead to incorrect deserialization or runtime errors if the JSON structure has additional properties or a different property name. A more robust approach using JsonDocument would ensure the entire object is consumed correctly and allow for explicit property checking.

        if (reader.TokenType == JsonTokenType.StartObject)
        {
            using var jsonDoc = JsonDocument.ParseValue(ref reader);
            if (jsonDoc.RootElement.TryGetProperty("Value", out var valueElement) && valueElement.ValueKind == JsonValueKind.String)
            {
                return new LocalizedString(valueElement.GetString());
            }
        }


/// <inheritdoc/>
public bool CanBuildComponent(PropertyInfo propertyInfo) => propertyInfo.PropertyType == typeof(LocalizedString);
public bool CanBuildComponent(PropertyInfo propertyInfo) => propertyInfo.PropertyType == typeof(LocalizedString?);

Choose a reason for hiding this comment

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

medium

This change restricts the component builder to only handle nullable LocalizedString properties (LocalizedString?). This might be a regression if you have non-nullable LocalizedString properties that should be rendered by this builder. To support both nullable and non-nullable types, the check should be broadened.

    public bool CanBuildComponent(PropertyInfo propertyInfo) => Nullable.GetUnderlyingType(propertyInfo.PropertyType) == typeof(LocalizedString) || propertyInfo.PropertyType == typeof(LocalizedString);

@sven-n
Copy link
Member

sven-n commented Jan 29, 2026

Thanks! I somehow didn't see this problems during testing, but I can reproduce it when I reinitialize the database.
To fix the Json serialization, it's enough to add the JsonConverter to the JsonSerializerOptions. Unfortunately, I missed 3 out of 4 places in the PlugInConfigurationExtensions.
Additionally, I probably need some additional checks in the game logic code to check for empty LocalizedStrings before sending messages to players 😅
I'll add a fresh PR to address these problems.

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.

2 participants