Skip to content
Merged
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
18 changes: 17 additions & 1 deletion src/Core/Configurations/RuntimeConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,24 @@ public void ValidateRelationshipsInConfig(RuntimeConfig runtimeConfig, IMetadata
string databaseName = runtimeConfig.GetDataSourceNameFromEntityName(entityName);
ISqlMetadataProvider sqlMetadataProvider = sqlMetadataProviderFactory.GetMetadataProvider(databaseName);

foreach (EntityRelationship relationship in entity.Relationships!.Values)
// Dictionary to store mapping from target entity's name to relationship name. Whenever we encounter that we
// are getting more than 1 entry for a target entity, we throw a validation error as it indicates the user has
// defined multiple relationships between the same source and target entities.
Dictionary<string, string> targetEntityNameToRelationshipName = new();
foreach ((string relationshipName, EntityRelationship relationship) in entity.Relationships!)
{
string targetEntityName = relationship.TargetEntity;
if (targetEntityNameToRelationshipName.TryGetValue(targetEntityName, out string? duplicateRelationshipName))
{
HandleOrRecordException(new DataApiBuilderException(
message: $"Defining multiple relationships: {duplicateRelationshipName}, {relationshipName} between source entity: {entityName} and target entity: {targetEntityName} is not supported.",
statusCode: HttpStatusCode.ServiceUnavailable,
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError));
}

// Add entry for this relationship to the dictionary tracking all the relationships for this entity.
targetEntityNameToRelationshipName[targetEntityName] = relationshipName;

// Validate if entity referenced in relationship is defined in the config.
if (!runtimeConfig.Entities.ContainsKey(relationship.TargetEntity))
{
Expand Down
104 changes: 104 additions & 0 deletions src/Service.Tests/Unittests/ConfigValidationUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,110 @@ public void TestAddingRelationshipWithDisabledGraphQL()
Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode);
}

/// <summary>
/// Testing the RuntimeCOnfigValidator.ValidateRelationshipsInConfig() method to ensure that we throw a validation error
/// when GraphQL is enabled on the source entity and the user defines multiple relationships between the same source and target entities.
/// </summary>
[DataTestMethod]
[DataRow(true, DisplayName = "Validate that an exception is thrown when GQL is enabled and user defines multiple relationship between source and target entities.")]
[DataRow(false, DisplayName = "Validate that no exception is thrown when GQL is disabled and user defines multiple relationship between source and target entities.")]
public void TestMultipleRelationshipsBetweenSourceAndTargetEntities(bool isGQLEnabledForSource)
{
string sourceEntityName = "SourceEntity", targetEntityName = "TargetEntity";

// Create relationship between source and target entities.
EntityRelationship relationship = new(
Cardinality: Cardinality.One,
TargetEntity: targetEntityName,
SourceFields: new string[] { "abc" },
TargetFields: new string[] { "xyz" },
LinkingObject: null,
LinkingSourceFields: null,
LinkingTargetFields: null
);

// Add another relationship between the same source and target entities.
EntityRelationship duplicateRelationship = new(
Cardinality: Cardinality.Many,
TargetEntity: targetEntityName,
SourceFields: null,
TargetFields: null,
LinkingObject: null,
LinkingSourceFields: null,
LinkingTargetFields: null
);

string relationshipName = "relationship", duplicateRelationshipName = "duplicateRelationship";
Dictionary<string, EntityRelationship> relationshipMap = new()
{
{ relationshipName, relationship },
{ duplicateRelationshipName, duplicateRelationship }
};

// Creating source entity with enabled graphQL
Entity sourceEntity = GetSampleEntityUsingSourceAndRelationshipMap(
source: "TEST_SOURCE1",
relationshipMap: relationshipMap,
graphQLDetails: new(Singular: "", Plural: "", Enabled: isGQLEnabledForSource)
);

// Creating target entity.
Entity targetEntity = GetSampleEntityUsingSourceAndRelationshipMap(
source: "TEST_SOURCE2",
relationshipMap: null,
graphQLDetails: new("", "", true)
);

Dictionary<string, Entity> entityMap = new()
{
{ sourceEntityName, sourceEntity },
{ targetEntityName, targetEntity }
};

RuntimeConfig runtimeConfig = new(
Schema: "UnitTestSchema",
DataSource: new DataSource(DatabaseType: DatabaseType.MSSQL, "", Options: null),
Runtime: new(
Rest: new(),
GraphQL: new(),
Host: new(null, null)
),
Entities: new(entityMap)
);

RuntimeConfigValidator configValidator = InitializeRuntimeConfigValidator();
Mock<ISqlMetadataProvider> _sqlMetadataProvider = new();
Dictionary<string, DatabaseObject> mockDictionaryForEntityDatabaseObject = new()
{
{
sourceEntityName,
new DatabaseTable("dbo", "TEST_SOURCE1")
},

{
targetEntityName,
new DatabaseTable("dbo", "TEST_SOURCE2")
}
};

_sqlMetadataProvider.Setup(x => x.EntityToDatabaseObject).Returns(mockDictionaryForEntityDatabaseObject);
Mock<IMetadataProviderFactory> _metadataProviderFactory = new();
_metadataProviderFactory.Setup(x => x.GetMetadataProvider(It.IsAny<string>())).Returns(_sqlMetadataProvider.Object);

if (isGQLEnabledForSource)
{
// Assert for expected exception.
DataApiBuilderException ex = Assert.ThrowsException<DataApiBuilderException>(() =>
configValidator.ValidateRelationshipsInConfig(runtimeConfig, _metadataProviderFactory.Object));
Assert.AreEqual($"Defining multiple relationships: {relationshipName}, {duplicateRelationshipName} between source entity: {sourceEntityName} and target entity: {targetEntityName} is not supported.", ex.Message);
Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode);
}
else
{
configValidator.ValidateRelationshipsInConfig(runtimeConfig, _metadataProviderFactory.Object);
}
}

/// <summary>
/// Test method to check that an exception is thrown when LinkingObject was provided
/// while either LinkingSourceField or SourceField is null, and either targetFields or LinkingTargetField is null.
Expand Down