Skip to content

Commit 77b1524

Browse files
committed
Expose source text on parser base class
1 parent 65baa1d commit 77b1524

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/JsonApiDotNetCore/Queries/Parsing/IncludeParser.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ public IncludeExpression Parse(string source, ResourceType resourceType)
2929

3030
Tokenize(source);
3131

32-
IncludeExpression expression = ParseInclude(source, resourceType);
32+
IncludeExpression expression = ParseInclude(resourceType);
3333

3434
AssertTokenStackIsEmpty();
3535
ValidateMaximumIncludeDepth(expression, 0);
3636

3737
return expression;
3838
}
3939

40-
protected virtual IncludeExpression ParseInclude(string source, ResourceType resourceType)
40+
protected virtual IncludeExpression ParseInclude(ResourceType resourceType)
4141
{
42-
ArgumentNullException.ThrowIfNull(source);
4342
ArgumentNullException.ThrowIfNull(resourceType);
4443

4544
var treeRoot = IncludeTreeNode.CreateRoot(resourceType);
@@ -56,13 +55,13 @@ protected virtual IncludeExpression ParseInclude(string source, ResourceType res
5655
isAtStart = false;
5756
}
5857

59-
ParseRelationshipChain(source, treeRoot);
58+
ParseRelationshipChain(treeRoot);
6059
}
6160

6261
return treeRoot.ToExpression();
6362
}
6463

65-
private void ParseRelationshipChain(string source, IncludeTreeNode treeRoot)
64+
private void ParseRelationshipChain(IncludeTreeNode treeRoot)
6665
{
6766
// A relationship name usually matches a single relationship, even when overridden in derived types.
6867
// But in the following case, two relationships are matched on GET /shoppingBaskets?include=items:
@@ -90,30 +89,29 @@ private void ParseRelationshipChain(string source, IncludeTreeNode treeRoot)
9089
// that there's currently no way to include Products without Articles. We could add such optional upcast syntax
9190
// in the future, if desired.
9291

93-
ReadOnlyCollection<IncludeTreeNode> children = ParseRelationshipName(source, [treeRoot]);
92+
ReadOnlyCollection<IncludeTreeNode> children = ParseRelationshipName([treeRoot]);
9493

9594
while (TokenStack.TryPeek(out Token? nextToken) && nextToken.Kind == TokenKind.Period)
9695
{
9796
EatSingleCharacterToken(TokenKind.Period);
9897

99-
children = ParseRelationshipName(source, children);
98+
children = ParseRelationshipName(children);
10099
}
101100
}
102101

103-
private ReadOnlyCollection<IncludeTreeNode> ParseRelationshipName(string source, IReadOnlyCollection<IncludeTreeNode> parents)
102+
private ReadOnlyCollection<IncludeTreeNode> ParseRelationshipName(IReadOnlyCollection<IncludeTreeNode> parents)
104103
{
105104
int position = GetNextTokenPositionOrEnd();
106105

107106
if (TokenStack.TryPop(out Token? token) && token.Kind == TokenKind.Text)
108107
{
109-
return LookupRelationshipName(token.Value!, parents, source, position);
108+
return LookupRelationshipName(token.Value!, parents, position);
110109
}
111110

112111
throw new QueryParseException("Relationship name expected.", position);
113112
}
114113

115-
private static ReadOnlyCollection<IncludeTreeNode> LookupRelationshipName(string relationshipName, IReadOnlyCollection<IncludeTreeNode> parents,
116-
string source, int position)
114+
private ReadOnlyCollection<IncludeTreeNode> LookupRelationshipName(string relationshipName, IReadOnlyCollection<IncludeTreeNode> parents, int position)
117115
{
118116
List<IncludeTreeNode> children = [];
119117
HashSet<RelationshipAttribute> relationshipsFound = [];
@@ -135,7 +133,7 @@ private static ReadOnlyCollection<IncludeTreeNode> LookupRelationshipName(string
135133
}
136134

137135
AssertRelationshipsFound(relationshipsFound, relationshipName, parents, position);
138-
AssertAtLeastOneCanBeIncluded(relationshipsFound, relationshipName, source, position);
136+
AssertAtLeastOneCanBeIncluded(relationshipsFound, relationshipName, position);
139137

140138
return children.AsReadOnly();
141139
}
@@ -201,15 +199,15 @@ private static string GetErrorMessageForNoneFound(string relationshipName, Resou
201199
return builder.ToString();
202200
}
203201

204-
private static void AssertAtLeastOneCanBeIncluded(HashSet<RelationshipAttribute> relationshipsFound, string relationshipName, string source, int position)
202+
private void AssertAtLeastOneCanBeIncluded(HashSet<RelationshipAttribute> relationshipsFound, string relationshipName, int position)
205203
{
206204
if (relationshipsFound.All(relationship => relationship.IsIncludeBlocked()))
207205
{
208206
ResourceType resourceType = relationshipsFound.First().LeftType;
209207
string message = $"Including the relationship '{relationshipName}' on '{resourceType}' is not allowed.";
210208

211209
var exception = new QueryParseException(message, position);
212-
string specificMessage = exception.GetMessageWithPosition(source);
210+
string specificMessage = exception.GetMessageWithPosition(Source);
213211

214212
throw new InvalidQueryStringParameterException("include", "The specified include is invalid.", specificMessage);
215213
}

src/JsonApiDotNetCore/Queries/Parsing/QueryExpressionParser.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ namespace JsonApiDotNetCore.Queries.Parsing;
1818
[PublicAPI]
1919
public abstract class QueryExpressionParser
2020
{
21-
private int _endOfSourcePosition;
22-
2321
/// <summary>
2422
/// Contains the tokens produced from the source text, after <see cref="Tokenize" /> has been called.
2523
/// </summary>
@@ -28,6 +26,11 @@ public abstract class QueryExpressionParser
2826
/// </remarks>
2927
protected Stack<Token> TokenStack { get; private set; } = new();
3028

29+
/// <summary>
30+
/// Contains the source text that tokens were produced from, after <see cref="Tokenize" /> has been called.
31+
/// </summary>
32+
protected string Source { get; private set; } = string.Empty;
33+
3134
/// <summary>
3235
/// Enables derived types to throw a <see cref="QueryParseException" /> when usage of a JSON:API field inside a field chain is not permitted.
3336
/// </summary>
@@ -45,9 +48,10 @@ protected virtual void Tokenize(string source)
4548
{
4649
ArgumentNullException.ThrowIfNull(source);
4750

51+
Source = source;
52+
4853
var tokenizer = new QueryTokenizer(source);
4954
TokenStack = new Stack<Token>(tokenizer.EnumerateTokens().Reverse());
50-
_endOfSourcePosition = source.Length;
5155
}
5256

5357
/// <summary>
@@ -154,7 +158,7 @@ protected int GetNextTokenPositionOrEnd()
154158
return nextToken.Position;
155159
}
156160

157-
return _endOfSourcePosition;
161+
return Source.Length;
158162
}
159163

160164
/// <summary>

0 commit comments

Comments
 (0)