Skip to content

Commit 8f226ea

Browse files
committed
Remove $CHILD in favor for using either $SELF or direct property access
1 parent 5ca64cd commit 8f226ea

8 files changed

Lines changed: 34 additions & 35 deletions

File tree

shared/ParameterSources/ItemParameterSource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ internal sealed class ItemParameterSource : ActionParameterSourceDefinition<Item
55
{
66
public const string IndexPropertyName = "$INDEX";
77
public const string ParentPropertyName = "$PARENT";
8-
public const string ChildPropertyName = "$CHILD";
98
}
109
}

src/Dibix.Http.Server/Runtime/HttpParameterResolver.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,7 @@ private static Expression CollectSourcePropertyValue(IHttpActionDescriptor actio
513513
for (int i = 0; i < parts.Length; i++)
514514
{
515515
string propertyName = parts[i];
516-
if (propertyName == SelfPropertyName)
517-
continue;
518-
516+
bool isNestedEnumerablePair = IsNestedEnumerablePair(value);
519517
bool isItemParameter = parameter.Items != null;
520518
bool isNestedProperty = i > 0;
521519
bool hasNestedProperties = parts.Length > 1;
@@ -527,12 +525,14 @@ private static Expression CollectSourcePropertyValue(IHttpActionDescriptor actio
527525
{
528526
Expression sourcePropertyExpression = null;
529527
Type resultEnumerableType = null;
528+
bool isNestedEnumerable = false;
530529
if (isNestedProperty)
531530
{
532531
Type propertyEnumerableType = TryGetEnumerableType(value.Type);
533532
if (propertyEnumerableType != null)
534533
{
535534
sourcePropertyExpression = BuildFlattenNestedEnumerableExpression(value, propertyEnumerableType, propertyPath, parts, i, out resultEnumerableType);
535+
isNestedEnumerable = true;
536536

537537
// Skip to the end since we've processed the entire enumerable path
538538
i = parts.Length - 1;
@@ -553,15 +553,14 @@ private static Expression CollectSourcePropertyValue(IHttpActionDescriptor actio
553553
}
554554

555555
resultEnumerableType ??= GetEnumerableType(sourcePropertyExpression.Type);
556-
value = CollectItemsParameterValue(action, requestParameter, argumentsParameter, dependencyResolverParameter, actionParameter, compilationContext, parameter, sourcePropertyExpression, resultEnumerableType, sourceMap, ensureNullPropagation);
556+
value = CollectItemsParameterValue(action, requestParameter, argumentsParameter, dependencyResolverParameter, actionParameter, compilationContext, parameter, sourcePropertyExpression, resultEnumerableType, sourceMap, ensureNullPropagation, isNestedEnumerable);
557557
}
558558
else
559559
{
560-
bool isNestedEnumerablePair = IsNestedEnumerablePair(value);
561560
if (isNestedEnumerablePair)
562561
nestedEnumerableAnchor = value;
563562

564-
CollectSourcePropertyValue(propertyName, previousPropertyName, nestedEnumerableAnchor, ref value, ref nullCheckTarget);
563+
CollectSourcePropertyValue(propertyName, previousPropertyName, isNestedEnumerablePair, nestedEnumerableAnchor, ref value, ref nullCheckTarget);
565564
}
566565

567566
if (ensureNullPropagation && nullCheckTarget != null && i + 1 < parts.Length)
@@ -579,18 +578,21 @@ private static Expression CollectSourcePropertyValue(IHttpActionDescriptor actio
579578

580579
return value;
581580
}
582-
private static void CollectSourcePropertyValue(string propertyName, string previousPropertyName, Expression nestedEnumerableAnchor, ref Expression value, ref Expression nullCheckTarget)
581+
private static void CollectSourcePropertyValue(string propertyName, string previousPropertyName, bool isNestedEnumerablePair, Expression nestedEnumerableAnchor, ref Expression value, ref Expression nullCheckTarget)
583582
{
584583
switch (propertyName)
585584
{
586585
case ItemParameterSource.ParentPropertyName:
587586
value = Expression.Property(value, nameof(NestedEnumerablePair<object, object>.Parent));
588587
break;
589588

590-
case ItemParameterSource.ChildPropertyName:
589+
case SelfPropertyName when isNestedEnumerablePair:
591590
value = Expression.Property(value, nameof(NestedEnumerablePair<object, object>.Child));
592591
break;
593592

593+
case SelfPropertyName:
594+
break;
595+
594596
case ItemParameterSource.IndexPropertyName:
595597
{
596598
if (nestedEnumerableAnchor == null)
@@ -599,16 +601,15 @@ private static void CollectSourcePropertyValue(string propertyName, string previ
599601
string indexPropertyName = previousPropertyName switch
600602
{
601603
ItemParameterSource.ParentPropertyName => nameof(NestedEnumerablePair<object, object>.ParentIndex),
602-
ItemParameterSource.ChildPropertyName => nameof(NestedEnumerablePair<object, object>.ChildIndex),
603-
_ => throw new InvalidOperationException($"Property '{ItemParameterSource.IndexPropertyName}' cannot be accessed on type '{value.Type}'")
604+
_ => nameof(NestedEnumerablePair<object, object>.ChildIndex),
604605
};
605606
value = Expression.Property(nestedEnumerableAnchor, indexPropertyName);
606607
break;
607608
}
608609

609610
default:
610611
{
611-
if (IsNestedEnumerablePair(value))
612+
if (isNestedEnumerablePair)
612613
{
613614
value = Expression.Property(value, nameof(NestedEnumerablePair<object, object>.Child));
614615
}
@@ -632,6 +633,7 @@ IHttpActionDescriptor action
632633
, Type itemType
633634
, IDictionary<string, Expression> sourceMap
634635
, bool ensureNullPropagation
636+
, bool isNestedEnumerable
635637
)
636638
{
637639
Guard.IsNotNull(itemType, nameof(itemType));
@@ -647,7 +649,7 @@ IHttpActionDescriptor action
647649
{
648650
HttpParameterInfo itemParameter;
649651
ParameterExpression itemSourceVariable;
650-
if (itemSource.SourceKind == HttpParameterSourceKind.SourceProperty && itemSource.Source.PropertyPath == ItemParameterSource.IndexPropertyName) // ITEM.$INDEX => i
652+
if (itemSource.SourceKind == HttpParameterSourceKind.SourceProperty && itemSource.Source.PropertyPath == ItemParameterSource.IndexPropertyName && !isNestedEnumerable) // ITEM.$INDEX => i
651653
{
652654
HttpParameterSourceInfo source = new HttpParameterSourceInfo(action, requestParameter, argumentsParameter, dependencyResolverParameter, actionParameter, compilationContext, sourceMap, ItemSourceName, sourceProvider: null, propertyPath: null);
653655
itemParameter = HttpParameterInfo.SourceInstance(itemSource.ParameterType, itemSource.InternalParameterName, source);

src/Dibix.Sdk.CodeGeneration/Registration/PropertyPathParameterSourceReader.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,7 @@ private void CollectItemPropertySourceNodes(ActionParameterPropertySourceBuilder
122122
while (!currentNode.Property.Type.IsEnumerable);
123123
continue;
124124

125-
case ItemParameterSource.ChildPropertyName:
126-
continue;
127-
128125
case ItemParameterSource.IndexPropertyName:
129-
case nameof(NestedEnumerablePair<object, object>.ParentIndex):
130-
case nameof(NestedEnumerablePair<object, object>.ChildIndex):
131126
break;
132127

133128
default:

tests/Dibix.Http.Server.Tests/HttpParameterResolverTest.Base.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,14 @@ private sealed class ExplicitHttpBodyItemNestedChildSet : StructuredType<Explici
268268
{
269269
public override string TypeName => "z";
270270

271-
public void Add(int itemid, int anotherid, int nestedchildid) => AddRecord(itemid, anotherid, nestedchildid);
271+
public void Add(int itemid, int anotherid, int nestedchildid, int nestedchildindex) => AddRecord(itemid, anotherid, nestedchildid, nestedchildindex);
272272

273273
protected override void CollectMetadata(ISqlMetadataCollector collector)
274274
{
275275
collector.RegisterMetadata("itemid", SqlDbType.Int);
276276
collector.RegisterMetadata("anotherid", SqlDbType.Int);
277277
collector.RegisterMetadata("nestedchildid", SqlDbType.Int);
278+
collector.RegisterMetadata("nestedchildindex", SqlDbType.Int);
278279
}
279280
}
280281

tests/Dibix.Http.Server.Tests/HttpParameterResolverTest.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,19 @@ public void Compile_ExplicitBodySource()
142142
x.ResolveParameterFromSource("childrena_", "BODY", "ItemsA.Child.Children", action =>
143143
{
144144
action.ResolveParameterFromSource("itemid", "ITEM", "$PARENT.$INDEX");
145-
action.ResolveParameterFromSource("childid", "ITEM", "$CHILD.Id");
145+
action.ResolveParameterFromSource("childid", "ITEM", "Id");
146146
});
147147
x.ResolveParameterFromSource("primitivechildrena_", "BODY", "ItemsA.Child.PrimitiveChildren", action =>
148148
{
149149
action.ResolveParameterFromSource("itemid", "ITEM", "$PARENT.$INDEX");
150-
action.ResolveParameterFromSource("childid", "ITEM", "$CHILD");
150+
action.ResolveParameterFromSource("childid", "ITEM", "$SELF");
151151
});
152152
x.ResolveParameterFromSource("nestedchildrena_", "BODY", "ItemsA.Child.Children.NestedChildren", action =>
153153
{
154154
action.ResolveParameterFromSource("itemid", "ITEM", "$PARENT.$PARENT.Id");
155155
action.ResolveParameterFromSource("anotherid", "ITEM", "$PARENT.AnotherId");
156-
action.ResolveParameterFromSource("nestedchildid", "ITEM", "$CHILD.Id");
156+
action.ResolveParameterFromSource("nestedchildid", "ITEM", "Id");
157+
action.ResolveParameterFromSource("nestedchildindex", "ITEM", "$INDEX");
157158
});
158159
});
159160
Assert.AreEqual(0, action.RequiredClaims.Count, "action.RequiredClaims.Count");
@@ -263,14 +264,14 @@ 1 56
263264
2 65
264265
2 66 ", primitivechildrena_.Dump());
265266
StructuredType nestedchildrena_ = AssertIsType<ExplicitHttpBodyItemNestedChildSet>(arguments["nestedchildrena_"]);
266-
Assert.AreEqual(@"itemid INT(4) anotherid INT(4) nestedchildid INT(4)
267-
------------- ---------------- --------------------
268-
5 55 511
269-
5 55 512
270-
5 56 521
271-
5 56 522
272-
6 65 611
273-
6 65 612 ", nestedchildrena_.Dump());
267+
Assert.AreEqual(@"itemid INT(4) anotherid INT(4) nestedchildid INT(4) nestedchildindex INT(4)
268+
------------- ---------------- -------------------- -----------------------
269+
5 55 511 1
270+
5 55 512 2
271+
5 56 521 1
272+
5 56 522 2
273+
6 65 611 1
274+
6 65 612 2 ", nestedchildrena_.Dump());
274275
Assert.AreEqual(5, arguments["skip"]);
275276
Assert.AreEqual(null, arguments["take"]);
276277
dependencyResolver.VerifyAll();

tests/Dibix.Http.Server.Tests/Resources/Compile_ExplicitBodySource.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,6 @@
127127
.Call $x.Add(
128128
(($y.Parent).Parent).Id,
129129
(($y.Parent).Child).AnotherId,
130-
($y.Child).Id)
130+
($y.Child).Id,
131+
$y.ChildIndex)
131132
}

tests/Dibix.Sdk.Tests.Database/Endpoints/GenericEndpoint.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@
162162
"source": "BODY.SomeIds.Child.Items.SubItems",
163163
"items": {
164164
"id1": "ITEM.$PARENT.$PARENT.Value",
165-
"id2": "ITEM.$CHILD.Id"
165+
"id2": "ITEM.Id"
166166
}
167167
},
168168
"primitivenested": {
169169
"source": "BODY.SomeIds.Child.PrimitiveIds",
170170
"items": {
171171
"id1": "ITEM.$PARENT.Id",
172-
"id2": "ITEM.$CHILD"
172+
"id2": "ITEM.$SELF"
173173
}
174174
},
175175
"g": "BODY.Data.Name"

tests/Dibix.Sdk.Tests/Resources/CodeGeneration/Endpoints.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,12 @@ public override void Configure(IHttpApiDiscoveryContext context)
550550
action.ResolveParameterFromSource("nested", "BODY", "SomeIds.Child.Items.SubItems", items =>
551551
{
552552
items.ResolveParameterFromSource("id1", "ITEM", "$PARENT.$PARENT.Value");
553-
items.ResolveParameterFromSource("id2", "ITEM", "$CHILD.Id");
553+
items.ResolveParameterFromSource("id2", "ITEM", "Id");
554554
});
555555
action.ResolveParameterFromSource("primitivenested", "BODY", "SomeIds.Child.PrimitiveIds", items =>
556556
{
557557
items.ResolveParameterFromSource("id1", "ITEM", "$PARENT.Id");
558-
items.ResolveParameterFromSource("id2", "ITEM", "$CHILD");
558+
items.ResolveParameterFromSource("id2", "ITEM", "$SELF");
559559
});
560560
action.ResolveParameterFromSource("g", "BODY", "Data.Name");
561561
});

0 commit comments

Comments
 (0)