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

Add YAML.safe_load config option #1668

Merged
merged 3 commits into from
Dec 31, 2024
Merged

Conversation

dzirtusss
Copy link
Contributor

@dzirtusss dzirtusss commented Dec 30, 2024

Summary

Pull Request checklist

Remove this line after checking all the items here. If the item is not applicable to the PR, both check it out and wrap it by ~.

  • Add/update test to cover these changes
  • Update documentation
  • Update CHANGELOG file
    Add the CHANGELOG entry at the top of the file.

Other Information

Remove this paragraph and mention any other important and relevant information such as benchmarks.


This change is Reviewable

Summary by CodeRabbit

  • New Features

    • Added support for customizable YAML safe loading options in internationalization (i18n) configurations
    • Introduced streaming server rendering support with new helper method stream_react_component
    • Enhanced console log replay during server rendering
  • Improvements

    • Updated error handling for YAML parsing and server-side rendering
    • Renamed registerStore method to registerStoreGenerators
  • Documentation

    • Updated configuration and i18n guides with new YAML loading options

Copy link
Contributor

coderabbitai bot commented Dec 30, 2024

Walkthrough

The pull request introduces enhancements to the internationalization (i18n) configuration in the React on Rails library. A new attribute i18n_yml_safe_load_options is added to the configuration, allowing more flexible YAML file loading. The changes modify how translations are generated and loaded, with a focus on providing customizable safe load options for YAML files. A new test case is added to verify the handling of YAML files with symbol-based translations, and sample locale files are introduced to support this functionality.

Changes

File Change Summary
lib/react_on_rails/configuration.rb Added i18n_yml_safe_load_options attribute to configuration and updated initialize method.
lib/react_on_rails/locales/base.rb Updated YAML loading to use configurable safe load options and improved error handling.
spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml Added German locale translation for :hello.
spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml Created new English locale file with various symbol translations.
spec/react_on_rails/locales_to_js_spec.rb Added test case for handling symbol-based YAML translations.
CHANGELOG.md Updated to reflect changes in configuration and new features.
docs/guides/configuration.md Added documentation for i18n_yml_safe_load_options.
docs/guides/i18n.md Updated i18n documentation to include new YAML handling instructions.

Assessment against linked issues

Objective Addressed Explanation
Handle Psych::DisallowedClass during locale file conversion to JS (#798)
Introduce customizable YAML loading options

Possibly related PRs

  • Enable use as a Git dependency #1664: This PR updates the CHANGELOG.md to include a new configuration option config.i18n_yml_safe_load_options, which is directly related to the changes made in the main PR regarding the addition of the i18n_yml_safe_load_options attribute in the Configuration class.

Suggested reviewers

  • AbanoubGhadban

Poem

🐰 A rabbit's tale of i18n delight,
Where YAML loads with configurable might!
Symbols dance, translations sing,
Flexible loading is now the thing.
Code hops forward with linguistic grace! 🌍


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dzirtusss dzirtusss marked this pull request as draft December 30, 2024 18:44
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
spec/react_on_rails/locales_to_js_spec.rb (1)

101-106: Ensure consistency in test setup.

This block successfully configures the new i18n_yml_safe_load_options. Consider verifying that other test contexts also reset or override these values to prevent side effects across tests.

lib/react_on_rails/configuration.rb (1)

73-73: Keyword parameter inclusion.

Accepting i18n_yml_safe_load_options in the initializer helps centralize configuration within Configuration. Make sure to document this new parameter in the project's README or CHANGELOG.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4339d30 and 977b131.

📒 Files selected for processing (5)
  • lib/react_on_rails/configuration.rb (3 hunks)
  • lib/react_on_rails/locales/base.rb (1 hunks)
  • spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml (1 hunks)
  • spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml (1 hunks)
  • spec/react_on_rails/locales_to_js_spec.rb (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml
🔇 Additional comments (7)
spec/react_on_rails/locales_to_js_spec.rb (3)

100-100: Clean separation of test context.

Introducing a dedicated example group describing "with symbols in yaml" is a clear, well-structured way to isolate this test scenario.


108-113: Good teardown practice.

Restoring the configuration to its previous state helps maintain independence between test examples.


115-117: Effective test coverage for symbol-based YAML.

This test confirms that loading locale files with symbol keys does not raise errors, ensuring the new option is functional.

lib/react_on_rails/locales/base.rb (1)

118-119: Leverage safe Load Options.

The introduction of safe_load_options effectively integrates user-defined YAML loading parameters. Consider adding a fallback or exception handling strategy if a needed class is not permitted.

lib/react_on_rails/configuration.rb (2)

55-55: New configuration attribute recognized.

Adding :i18n_yml_safe_load_options to the attr_accessor allows flexible YAML loading settings.


84-84: Storing the YAML safe load options.

Assigning the incoming i18n_yml_safe_load_options to the instance variable ensures that user-defined loaders can be honored throughout the application.

spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml (1)

1-8: Useful YAML file for symbol testing.

Providing example keys (symbols) validates real-world usage of the new safe_load_options. The content is clear and sufficient for verifying symbol-based locales.

@dzirtusss dzirtusss marked this pull request as ready for review December 30, 2024 19:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
docs/guides/i18n.md (1)

34-37: Enhance documentation with security context and examples

The documentation would benefit from additional context about YAML safety. Consider expanding it to:

  1. Define what constitutes "unsafe YAML"
  2. Provide examples of unsafe YAML content
  3. Add a security note about carefully choosing permitted classes

Here's a suggested expansion:

-4. If your locale files (or one of the gems locale files) contains unsafe YAML, you may need to configure `config.i18n_yml_safe_load_options` if you can't fix such yamls properly.
+4. If your locale files (or one of the gems locale files) contains unsafe YAML (e.g., YAML with Ruby symbols or custom objects), you may need to configure `config.i18n_yml_safe_load_options` if you can't fix such yamls properly.
+
+   Example of unsafe YAML content:
+   ```yaml
+   en:
+     status: !ruby/symbol success
+     custom_object: !ruby/object:MyClass {}
+   ```
+
     ```rb
     config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
     ```
+
+   Note: Be cautious when permitting classes as it may pose security risks. Only permit classes that you trust and require.
docs/guides/configuration.md (1)

216-218: Add context and link to i18n guide

The configuration option would benefit from a brief explanation and a reference to the detailed documentation.

-  # Possible YAML.safe_load options pass-through for locales
+  # Options for YAML.safe_load when parsing locale files
+  # See docs/guides/i18n.md for detailed usage and security considerations
   # config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 977b131 and ce763cc.

📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • docs/guides/configuration.md (1 hunks)
  • docs/guides/i18n.md (1 hunks)
  • lib/react_on_rails/locales/base.rb (1 hunks)
  • spec/react_on_rails/locales_to_js_spec.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • spec/react_on_rails/locales_to_js_spec.rb
  • lib/react_on_rails/locales/base.rb
🔇 Additional comments (1)
CHANGELOG.md (1)

36-36: LGTM! Changelog entry is well-formatted and properly positioned.

The changelog entry follows the contribution guidelines, clearly describes the new feature, and includes proper attribution.

@justin808 justin808 merged commit 16c9132 into master Dec 31, 2024
11 checks passed
@justin808 justin808 deleted the sergey/add-yaml-safe-loading-config branch December 31, 2024 21:17
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.

Psych::DisallowedClass during some locale files convertion to js
2 participants