Sanitize autoentity-generated names to prevent invalid REST paths and GraphQL singular/plural tables for SQL objects with spaces#3609
Conversation
Agent-Logs-Url: https://github.com/Azure/data-api-builder/sessions/2051de6a-3432-4bd6-901c-3a3d6a8cc114 Co-authored-by: RubenCerna2079 <32799214+RubenCerna2079@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR improves MSSQL autoentity generation by sanitizing auto-generated entity names so SQL objects containing whitespace don’t produce invalid REST path segments or invalid GraphQL names during default singular/plural generation and config validation.
Changes:
- Add a whitespace-removal (camel-join) sanitizer for generated entity names and apply it during MSSQL autoentity generation.
- Extend MSSQL test schema with a table that contains spaces in its name and seed data for it.
- Update configuration tests to validate REST + GraphQL access for an autoentity generated from an object with spaces.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Service.Tests/DatabaseSchema-MsSql.sql | Adds an MSSQL table with whitespace in its name to exercise autoentity naming. |
| src/Service.Tests/Configuration/ConfigurationTests.cs | Extends autoentity end-to-end test to include an object with spaces and validates REST/GraphQL access. |
| src/Core/Services/MetadataProviders/SqlMetadataProvider.cs | Introduces SanitizeGeneratedEntityName helper for generated entity names. |
| src/Core/Services/MetadataProviders/MsSqlMetadataProvider.cs | Applies sanitization to autoentity-generated entityName before entity creation. |
| /// <summary> | ||
| /// Sanitizes the generated entity name by removing whitespace and capitalizing the next character after whitespace. | ||
| /// </summary> | ||
| /// <param name="name">The entity name to be sanitized.</param> | ||
| /// <returns>The sanitized entity name.</returns> | ||
| protected static string SanitizeGeneratedEntityName(string name) | ||
| { | ||
| StringBuilder sanitizedName = new(name.Length); | ||
| bool capitalizeNext = false; | ||
|
|
||
| foreach (char character in name) | ||
| { | ||
| if (char.IsWhiteSpace(character)) |
There was a problem hiding this comment.
should we sanitize other valid characters like []? for e.g.- [Customer-Orders] still produces dbo_Customer-Orders, which can be invalid for REST/GraphQL. if this is about only removing whitespaces, may be the function can be renamed with that.
| } | ||
|
|
||
| // Sanitize the entity name by ensuring all whitespace characters are removed. | ||
| entityName = SanitizeGeneratedEntityName(entityName); |
There was a problem hiding this comment.
I am looking at this section lately. consider we have 2 schema in database- Order Item and OrderItem. can we include the schema name along with the entityName/objectName in logs and response? since, santitized object will always show up as OederItem
Why make this change?
Tables with whitespace in names (for example,
Order Items) were being auto-generated into entity names likedbo_Order Items, which then produced invalid REST paths as well as invalid GraphQL names for singular/plural tables which failed config validation. This change ensures autoentity name generation does not pass whitespace through to REST path and GraphQL singular/plural tables defaults.{schema}/{object}verbatim; whitespace in object names breaksdab validate.What is this change?
SqlMetadataProviderand applied it to generatedentity_namebefore entity creation.dbo_Order Items→dbo_OrderItems.TestAutoentitiesGeneratedWithUnusualElementson theConfigurationTestsfile with cases for single/multiple spaces.How was this tested?
Sample Request(s)
dab auto-config add mydef --patterns.include "dbo.Order Items" --patterns.name "{schema}_{object}"dab validatedbo.[Order Items]dbo_OrderItems(no whitespace)