Feature/switch to system component model data annotations#132
Feature/switch to system component model data annotations#132
Conversation
…as a new dependency to replace com.csutil.model.jsonschema.JsonSchemaAttributes in a next step
…ystem.ComponentModel.DataAnnotations to allow migrating to System.ComponentModel without having to change legacy code
…ystem.ComponentModel attributes schould be used
There was a problem hiding this comment.
Summary of Changes
Hello @cs-util, 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 primarily focuses on modernizing the codebase by transitioning from custom data annotation attributes to the standard System.ComponentModel.DataAnnotations framework. This change improves code maintainability and leverages built-in .NET capabilities. Additionally, the pull request includes significant updates to Unity project files, reflecting an upgrade to a newer Unity editor version, and introduces a new Xunit mock attribute.
Highlights
- Standardized Data Annotations: Migrated custom data annotation attributes (such as
Description,Regex,MinMaxRange,Required, andInputLength) to leverage the standardSystem.ComponentModel.DataAnnotationsnamespace. This improves consistency and aligns with .NET best practices. - Backward Compatibility: Ensured existing custom attributes remain functional by marking them
[Obsolete]and having them inherit from and wrap theirSystem.ComponentModel.DataAnnotationscounterparts, providing a smooth transition path for consumers. - Unity Project Upgrade: Updated Unity project settings, package versions, and logs, indicating a significant upgrade of the Unity editor version. This includes numerous changes to player settings and package dependencies.
- New Xunit Mock Attribute: Introduced a mock
Xunit.Traitattribute to facilitate testing, particularly for categorizing tests. - Enhanced GUID Documentation: Added detailed documentation to the
GuidV2.NewGuid()method, explaining its part-random, part-sequential generation strategy, uniqueness properties, and important considerations for its use.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 is currently in preview and 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 to provide feedback.
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
-
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. ↩
There was a problem hiding this comment.
Code Review
This pull request refactors the codebase to use the standard System.ComponentModel.DataAnnotations attributes instead of custom-defined ones. This is a great improvement for standardization and maintainability.
I've found a few issues:
- A critical issue where
RangeAttributeis used for string length validation instead ofStringLengthAttribute. - A high-severity issue where a minimum length constraint was dropped during the attribute migration.
- A medium-severity issue regarding adding a new custom attribute to a system namespace.
| public string id { get; private set; } = Guid.NewGuid().ToString(); | ||
|
|
||
| [InputLength(2, 30)] | ||
| [System.ComponentModel.DataAnnotations.Range(2, 30)] |
There was a problem hiding this comment.
The [InputLength(2, 30)] attribute on the name string property was replaced with [System.ComponentModel.DataAnnotations.Range(2, 30)]. The RangeAttribute is intended for numeric types, not for string length validation. For string length, you should use StringLengthAttribute.
[System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 2)]|
|
||
| [Content(ContentFormat.password, "Lenght >= 6 & has A-Z a-z 0-9 ?!..")] | ||
| [InputLength(min: 6)] | ||
| [System.ComponentModel.DataAnnotations.Range(minimum: 6, maximum: 100)] |
There was a problem hiding this comment.
The [InputLength(min: 6)] attribute on the password string property was replaced with [System.ComponentModel.DataAnnotations.Range(minimum: 6, maximum: 100)]. The RangeAttribute is for numeric types and will not validate string length. You should use StringLengthAttribute instead.
[System.ComponentModel.DataAnnotations.StringLength(100, MinimumLength = 6)]| [Required] | ||
| [InputLength(2, 30)] | ||
| [System.ComponentModel.DataAnnotations.Required] | ||
| [System.ComponentModel.DataAnnotations.StringLength(30)] |
There was a problem hiding this comment.
The original [InputLength(2, 30)] attribute specified a minimum length of 2. The new [System.ComponentModel.DataAnnotations.StringLength(30)] only specifies the maximum length, so the minimum length constraint of 2 has been lost. To preserve the original validation logic, you should also set the MinimumLength property.
[System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 2)]| namespace System.ComponentModel.DataAnnotations { | ||
|
|
||
| [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] | ||
| public class MaxWordsAttribute : RegularExpressionAttribute { |
This commit adds a new `ValidateObjectAttribute` that extends `ValidationAttribute` to support validation of nested objects and collection elements when using `Validator.TryValidateObject`. Key points ---------- * Recursively invokes the Data-Annotations pipeline on referenced objects and (optionally) every `IEnumerable` element. * Propagates validation errors with fully-qualified property paths (e.g. `bestFriend.user.name`) so MVC/Blazor can map them to UI fields. * Handles both public properties **and** public fields, closing a gap in the default validator. * Exposes `ValidateCollectionElements` switch (default: `true`). * Sets `RequiresValidationContext = true` to preserve DI services and `Items` dictionary. * Added unit tests in `JsonSchemaTests.TestModelValidation` to cover: – valid model, – invalid simple properties, – invalid nested objects, – invalid collection elements.
This change is