Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions schemas/dab.draft.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@
},
"name": {
"type": "string",
"description": "Entity name interpolation pattern using {schema} and {object}. Null defaults to {object}. Must be unique for every entity inside the pattern",
"default": "{object}"
"description": "Entity name interpolation pattern using {schema} and {object}. Null defaults to {schema}_{object}. Must be unique for every entity inside the pattern",
"default": "{schema}_{object}"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Commands/AutoConfigOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public AutoConfigOptions(
[Option("patterns.exclude", Required = false, HelpText = "T-SQL LIKE pattern(s) to exclude database objects. Space-separated array of patterns. Default: null")]
public IEnumerable<string>? PatternsExclude { get; }

[Option("patterns.name", Required = false, HelpText = "Interpolation syntax for entity naming (must be unique for each generated entity). Default: '{object}'")]
[Option("patterns.name", Required = false, HelpText = "Interpolation syntax for entity naming (must be unique for each generated entity). Default: '{schema}_{object}'")]
public string? PatternsName { get; }

[Option("template.mcp.dml-tools", Required = false, HelpText = "Enable/disable DML tools for generated entities. Allowed values: true, false. Default: true")]
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ObjectModel/AutoentityPatterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public AutoentityPatterns(
}
else
{
this.Name = "{object}";
this.Name = "{schema}_{object}";
Comment thread
RubenCerna2079 marked this conversation as resolved.
Comment thread
RubenCerna2079 marked this conversation as resolved.
}
}

Expand Down
117 changes: 111 additions & 6 deletions src/Service.Tests/Configuration/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5592,12 +5592,12 @@ public async Task TestAutoentitiesAreGeneratedIntoEntities(bool useEntities, int
{
// Act
RuntimeConfigProvider configProvider = server.Services.GetService<RuntimeConfigProvider>();
using HttpRequestMessage restRequest = new(HttpMethod.Get, "/api/publishers");
using HttpRequestMessage restRequest = new(HttpMethod.Get, "/api/dbo_publishers");
using HttpResponseMessage restResponse = await client.SendAsync(restRequest);

string graphqlQuery = @"
{
publishers {
dbo_publishers {
items {
id
name
Expand Down Expand Up @@ -5635,6 +5635,111 @@ public async Task TestAutoentitiesAreGeneratedIntoEntities(bool useEntities, int
}
}

/// <summary>
/// This test validates that multiple autoentities with the same object name
/// but different schemas can be generated and accessed properly with the
/// default 'property.name' which should generate entities named '{schema}_{object}'.
/// </summary>
[TestCategory(TestCategory.MSSQL)]
[TestMethod]
public async Task TestAutoentitiesWithSameObjectDifferentSchemas()
{
// Arrange
Dictionary<string, Autoentity> autoentityMap = new()
{
{
"PublisherAutoEntity", new Autoentity(
Patterns: new AutoentityPatterns(
Include: null,
Exclude: null,
Name: null
),
Template: new AutoentityTemplate(
Rest: new EntityRestOptions(Enabled: true),
GraphQL: new EntityGraphQLOptions(
Singular: string.Empty,
Plural: string.Empty,
Enabled: true
),
Health: null,
Cache: null
),
Comment thread
RubenCerna2079 marked this conversation as resolved.
Permissions: new[] { GetMinimalPermissionConfig(AuthorizationResolver.ROLE_ANONYMOUS) }
)
}
};

// Create DataSource for MSSQL connection
DataSource dataSource = new(DatabaseType.MSSQL,
GetConnectionStringFromEnvironmentConfig(environment: TestCategory.MSSQL), Options: null);

// Build complete runtime configuration with autoentities
RuntimeConfig configuration = new(
Schema: "TestAutoentitiesSchema",
DataSource: dataSource,
Runtime: new(
Rest: new(Enabled: true),
GraphQL: new(Enabled: true),
Mcp: new(Enabled: false),
Host: new(
Cors: null,
Authentication: new Config.ObjectModel.AuthenticationOptions(
Provider: nameof(EasyAuthType.StaticWebApps),
Jwt: null
)
)
),
Entities: new(new Dictionary<string, Entity>()),
Autoentities: new RuntimeAutoentities(autoentityMap)
);

File.WriteAllText(CUSTOM_CONFIG_FILENAME, configuration.ToJson());

string[] args = new[] { $"--ConfigFileName={CUSTOM_CONFIG_FILENAME}" };

using (TestServer server = new(Program.CreateWebHostBuilder(args)))
using (HttpClient client = server.CreateClient())
{
// Act
using HttpRequestMessage restFooRequest = new(HttpMethod.Get, "/api/foo_magazines");
using HttpResponseMessage restFooResponse = await client.SendAsync(restFooRequest);

using HttpRequestMessage restBarRequest = new(HttpMethod.Get, "/api/bar_magazines");
using HttpResponseMessage restBarResponse = await client.SendAsync(restBarRequest);

string graphqlQuery = @"
{
foo_magazines {
items {
id
issue_number
}
}
bar_magazines {
items {
comic_name
issue
}
}
}";

object graphqlPayload = new { query = graphqlQuery };
HttpRequestMessage graphqlRequest = new(HttpMethod.Post, "/graphql")
{
Content = JsonContent.Create(graphqlPayload)
};
HttpResponseMessage graphqlResponse = await client.SendAsync(graphqlRequest);

// Assert
// Verify REST response
Assert.AreEqual(HttpStatusCode.OK, restFooResponse.StatusCode, "REST request to auto-generated entity 'foo_magazines' should succeed");
Assert.AreEqual(HttpStatusCode.OK, restBarResponse.StatusCode, "REST request to auto-generated entity 'bar_magazines' should succeed");

// Verify GraphQL response
Assert.AreEqual(HttpStatusCode.OK, graphqlResponse.StatusCode, "GraphQL request to auto-generated entity should succeed");
}
}

/// <summary>
///
/// </summary>
Expand All @@ -5646,10 +5751,10 @@ public async Task TestAutoentitiesAreGeneratedIntoEntities(bool useEntities, int
/// <returns></returns>
[TestCategory(TestCategory.MSSQL)]
[DataTestMethod]
[DataRow("publishers", "uniqueSingularPublisher", "uniquePluralPublishers", "/unique/publisher", "Entity 'publishers' conflicts with autoentity pattern 'PublisherAutoEntity'. Use --patterns.exclude to skip it.", DisplayName = "Autoentities fail due to entity name")]
[DataRow("UniquePublisher", "publishers", "uniquePluralPublishers", "/unique/publisher", "Entity publishers generates queries/mutation that already exist", DisplayName = "Autoentities fail due to graphql singular type")]
[DataRow("UniquePublisher", "uniqueSingularPublisher", "publishers", "/unique/publisher", "Entity publishers generates queries/mutation that already exist", DisplayName = "Autoentities fail due to graphql plural type")]
[DataRow("UniquePublisher", "uniqueSingularPublisher", "uniquePluralPublishers", "/publishers", "The rest path: publishers specified for entity: publishers is already used by another entity.", DisplayName = "Autoentities fail due to rest path")]
[DataRow("dbo_publishers", "uniqueSingularPublisher", "uniquePluralPublishers", "/unique/publisher", "Entity 'dbo_publishers' conflicts with autoentity pattern 'PublisherAutoEntity'. Use --patterns.exclude to skip it.", DisplayName = "Autoentities fail due to entity name")]
[DataRow("UniquePublisher", "dbo_publishers", "uniquePluralPublishers", "/unique/publisher", "Entity dbo_publishers generates queries/mutation that already exist", DisplayName = "Autoentities fail due to graphql singular type")]
[DataRow("UniquePublisher", "uniqueSingularPublisher", "dbo_publishers", "/unique/publisher", "Entity dbo_publishers generates queries/mutation that already exist", DisplayName = "Autoentities fail due to graphql plural type")]
[DataRow("UniquePublisher", "uniqueSingularPublisher", "uniquePluralPublishers", "/dbo_publishers", "The rest path: dbo_publishers specified for entity: dbo_publishers is already used by another entity.", DisplayName = "Autoentities fail due to rest path")]
public async Task ValidateAutoentityGenerationConflicts(string entityName, string singular, string plural, string path, string exceptionMessage)
{
// Arrange
Expand Down
Loading