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
86 changes: 86 additions & 0 deletions src/Cli.Tests/UpdateEntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,92 @@ public void TestUpdateEntityDescription()
Assert.AreEqual("Updated description", updatedRuntimeConfig.Entities["MyEntity"].Description);
}

/// <summary>
/// Updating a field's description should either preserve or clear
/// the existing primary-key flag depending on whether an explicit
/// --fields.primary-key value is provided.
/// </summary>
[DataTestMethod]
[DataRow(null, true, DisplayName = "No primary-key flag: preserve existing true")]
[DataRow(false, false, DisplayName = "Explicit primary-key false: clear existing true")]
public void TestUpdateFieldDescriptionPrimaryKeyBehavior(bool? primaryKeyFlag, bool expectedPrimaryKey)
{
string initialConfig = GetInitialConfigString() + "," + @"
""entities"": {
""MyEntity"": {
""source"": ""MyTable"",
""fields"": [
{
""name"": ""Id"",
""description"": ""Primary key"",
""primary-key"": true
}
],
""permissions"": [
{
""role"": ""anonymous"",
""actions"": [""read""]
}
]
}
}
}";

IEnumerable<bool>? primaryKeyFlags = primaryKeyFlag.HasValue
? new[] { primaryKeyFlag.Value }
: null;

UpdateOptions options = new(
source: null,
permissions: null,
entity: "MyEntity",
sourceType: null,
sourceParameters: null,
sourceKeyFields: null,
restRoute: null,
graphQLType: null,
fieldsToInclude: null,
fieldsToExclude: null,
policyRequest: null,
policyDatabase: null,
relationship: null,
cardinality: null,
targetEntity: null,
linkingObject: null,
linkingSourceFields: null,
linkingTargetFields: null,
relationshipFields: null,
map: null,
cacheEnabled: null,
cacheTtl: null,
config: TEST_RUNTIME_CONFIG_FILE,
restMethodsForStoredProcedure: null,
graphQLOperationForStoredProcedure: null,
description: null,
parametersNameCollection: null,
parametersDescriptionCollection: null,
parametersRequiredCollection: null,
parametersDefaultCollection: null,
fieldsNameCollection: new[] { "Id" },
fieldsAliasCollection: null,
fieldsDescriptionCollection: new[] { "Unique Key" },
fieldsPrimaryKeyCollection: primaryKeyFlags,
mcpDmlTools: null,
mcpCustomTool: null
);

Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(initialConfig, out RuntimeConfig? runtimeConfig), "Parsed config file.");
Assert.IsTrue(TryUpdateExistingEntity(options, runtimeConfig!, out RuntimeConfig updatedRuntimeConfig), "Successfully updated entity in the config.");

Entity updatedEntity = updatedRuntimeConfig.Entities["MyEntity"];
Assert.IsNotNull(updatedEntity.Fields);
Assert.AreEqual(1, updatedEntity.Fields!.Count);
FieldMetadata field = updatedEntity.Fields[0];
Assert.AreEqual("Id", field.Name);
Assert.AreEqual("Unique Key", field.Description);
Assert.AreEqual(expectedPrimaryKey, field.PrimaryKey);
}

private static string GetInitialConfigString()
{
return @"{" +
Expand Down
6 changes: 5 additions & 1 deletion src/Cli/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,7 @@ public static bool TryUpdateExistingEntity(UpdateOptions options, RuntimeConfig
List<FieldMetadata> updatedFieldsList = ComposeFieldsFromOptions(options);
Dictionary<string, FieldMetadata> updatedFieldsDict = updatedFieldsList.ToDictionary(f => f.Name, f => f);
List<FieldMetadata> mergedFields = [];
bool primaryKeyOptionProvided = options.FieldsPrimaryKeyCollection?.Any() == true;

foreach (FieldMetadata field in existingFields)
{
Expand All @@ -1757,7 +1758,10 @@ public static bool TryUpdateExistingEntity(UpdateOptions options, RuntimeConfig
Name = updatedField.Name,
Alias = updatedField.Alias ?? field.Alias,
Description = updatedField.Description ?? field.Description,
PrimaryKey = updatedField.PrimaryKey
// If --fields.primary-key was not provided at all,
// keep the existing primary-key flag. Otherwise,
// use the value coming from updatedField.
PrimaryKey = primaryKeyOptionProvided ? updatedField.PrimaryKey : field.PrimaryKey
});
updatedFieldsDict.Remove(field.Name); // Remove so only new fields remain
}
Expand Down
Loading