feat: enhance schema generation to support value types for additional…#19
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances JSON schema generation for generic types by using the value type's schema for additionalProperties instead of just true when the generic type has two or more type arguments (e.g., dictionaries).
Key changes:
- Modified
GetSchemamethod to check generic argument count and generate more descriptive schemas for dictionary-like types - Moved schema creation logic before the defines assignment to ensure proper schema structure
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (genericArgs.Length >= 2) | ||
| { | ||
| // Use the value type's schema for additionalProperties | ||
| var valueTypeSchema = GetSchema(reflector, genericArgs[1], defines); | ||
| schema = new JsonObject { [Type] = Object, [AdditionalProperties] = valueTypeSchema }; | ||
| } | ||
| else | ||
| { | ||
| schema = new JsonObject { [Type] = Object, [AdditionalProperties] = true }; | ||
| } | ||
|
|
||
| if (definesNeeded && !defineContainsType) | ||
| defines[typeId] = schema; |
There was a problem hiding this comment.
The schema assignment should occur before adding to defines to ensure the correct schema is stored. However, the current logic assigns the schema to defines[typeId] after creating it, but the original code at line 198 (now removed) was creating the schema after the defines assignment. This means defines[typeId] may now contain the wrong schema reference. The conditional check at line 196 should come after the schema is fully constructed but the code doesn't handle the case where definesNeeded might need the final schema that includes proper additionalProperties. Consider whether line 196-197 should remain in its current position or be moved after the schema construction is complete.
…schema generation
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
This pull request updates the logic for generating JSON schema definitions for generic types in the
GetSchemamethod. The main improvement is that the schema now uses the value type's schema foradditionalPropertieswhen the generic type has at least two type arguments, making the schema more accurate and descriptive.Enhancement to JSON schema generation:
JsonSchema.cs, theGetSchemamethod now checks if the generic type has two or more arguments and, if so, setsadditionalPropertiesin the schema to the value type's schema instead of justtrue. This provides a more precise schema for dictionaries and similar types.…Properties