Skip to content

Commit

Permalink
bugfix: fetching trait entities via graphql would not populate traite…
Browse files Browse the repository at this point in the history
…ntity fields that have a dot in their identifier
  • Loading branch information
maximiliancsuk committed May 23, 2024
1 parent 6ae619d commit cf70909
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
5 changes: 4 additions & 1 deletion backend/Omnikeeper/GraphQL/TraitEntities/TraitEntityTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ void Add(TraitAttribute ta, bool isRequired)
return null;
}
// ctx.FieldDefinition.Name is escaped (means "." is escaped as "__")
// while o.TraitAttributes is still unescaped
var fn = ctx.FieldDefinition.Name;
if (o.TraitAttributes.TryGetValue(fn, out var v))
var fnUnescaped = fn.Replace("__", "."); // HACK: this is not 100% failure proof as the actual escaping process does more than just escaping ".", see GenerateTraitAttributeFieldName()
if (o.TraitAttributes.TryGetValue(fnUnescaped, out var v))
{
return v.Attribute.Value.ToGraphQLValue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using NUnit.Framework;
using Omnikeeper.Base.Entity;
using Omnikeeper.Base.Model;
using System.Threading.Tasks;
using Tests.Integration.GraphQL.Base;

namespace Tests.Integration.GraphQL
{
class TraitEntityWithDotAttributeIdentifierTest : QueryTestBase
{
[Test]
public async Task TestBasics()
{
var userInDatabase = await SetupDefaultUser();
var (layerOkConfig, _) = await GetService<ILayerModel>().CreateLayerIfNotExists("__okconfig", ModelContextBuilder.BuildImmediate());
var (layer1, _) = await GetService<ILayerModel>().CreateLayerIfNotExists("layer_1", ModelContextBuilder.BuildImmediate());
var user = new AuthenticatedInternalUser(userInDatabase);

// force rebuild graphql schema
await ReinitSchema();

string mutationCreateTrait = @"
mutation {
manage_upsertRecursiveTrait(
trait: {
id: ""test_trait_a""
requiredAttributes: [
{
identifier: ""test.id""
template: {
name: ""test_trait_a.id""
type: TEXT
isID: true
isArray: false
valueConstraints: []
}
}
]
optionalAttributes: []
optionalRelations: [],
requiredTraits: []
}
) {
id
}
}
";
var expected1 = @"
{
""manage_upsertRecursiveTrait"":
{
""id"": ""test_trait_a""
}
}";
AssertQuerySuccess(mutationCreateTrait, expected1, user);

// force rebuild graphql schema
await ReinitSchema();

var queryTestTraitA = @"
{
traitEntities(layers: [""layer_1""]) {
test_trait_a {
all {
entity {
test__id
}
}
}
}
}
";
var expected2 = @"
{
""traitEntities"": {
""test_trait_a"": {
""all"": []
}
}
}
";
AssertQuerySuccess(queryTestTraitA, expected2, user);

// NOTE the double underscore intstead of a dot in the query
var mutationInsert = @"
mutation {
insertNew_test_trait_a(
layers: [""layer_1""]
writeLayer: ""layer_1""
ciName: ""Entity 1""
input: { test__id: ""entity_1"" }
) {
entity { test__id }
}
}
";

var expected3 = @"
{
""insertNew_test_trait_a"": {
""entity"": {
""test__id"": ""entity_1""
}
}
}
";
AssertQuerySuccess(mutationInsert, expected3, user);


var expected4 = @"
{
""traitEntities"": {
""test_trait_a"": {
""all"": [
{
""entity"": {
""test__id"": ""entity_1""
}
}
]
}
}
}
";
AssertQuerySuccess(queryTestTraitA, expected4, user);
}
}
}

0 comments on commit cf70909

Please sign in to comment.