Skip to content

Commit 96b23db

Browse files
committed
Enhance NamespaceParser to support '_' prefix for versioning and update tests accordingly
1 parent 6a22d46 commit 96b23db

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/Abstractions/src/Asp.Versioning.Abstractions/NamespaceParser.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,33 @@ protected virtual bool TryParse( Text identifier, out ApiVersion? apiVersion )
157157
return false;
158158
}
159159

160-
// 'v' | 'V' : [<year> : ['_'] : <month> : ['_'] : <day>] : ['_'] : [<major> ['_' : <minor>]] : ['_'] : [<status>]
160+
// 'v' | 'V' | '_' : [<year> : ['_'] : <month> : ['_'] : <day>] : ['_'] : [<major> ['_' : <minor>]] : ['_'] : [<status>]
161161
//
162162
// - v1
163163
// - v1_1
164164
// - v2_0_Beta
165165
// - v20180401
166166
// - v2018_04_01_1_1_Beta
167+
// - _2018_04_01
167168
var ch = identifier[0];
168169

169-
if ( ch != 'v' && ch != 'V' )
170+
if ( ch != 'v' && ch != 'V' && ch != '_' )
170171
{
171172
apiVersion = default;
172173
return false;
173174
}
174175

176+
if ( ch == '_' )
177+
{
178+
// '_' prefix requires the next character to be a digit;
179+
// otherwise, it's just a regular identifier
180+
if ( identifier.Length < 2 || !char.IsDigit( identifier[1] ) )
181+
{
182+
apiVersion = default;
183+
return false;
184+
}
185+
}
186+
175187
#if NETSTANDARD
176188
identifier = identifier.Substring( 1 );
177189
#else

src/Abstractions/test/Asp.Versioning.Abstractions.Tests/NamespaceParserTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public void parse_should_return_no_versions()
7070
{ "Contoso.Api.v2018_04_01_1_0_Beta.Controllers", "2018-04-01.1.0-Beta" },
7171
{ "MyRestaurant.Vegetarian.Food.v1_1.Controllers", "1.1" },
7272
{ "VersioningSample.V5.Controllers", "5.0" },
73+
{ "_1", "1" },
74+
{ "_1_1", "1.1" },
75+
{ "_20180401", "2018-04-01" },
76+
{ "_2018_04_01", "2018-04-01" },
77+
{ "_2018_04_01_Beta", "2018-04-01-Beta" },
78+
{ "_2018_04_01_1_0_Beta", "2018-04-01.1.0-Beta" },
79+
{ "Contoso.Api._2018_04_01.Controllers", "2018-04-01" },
80+
{ "Contoso.Api._1.Controllers", "1" },
81+
{ "Contoso.Api._1_1.Controllers", "1.1" },
82+
{ "Contoso.Api._0_9_Beta.Controllers", "0.9-Beta" },
7383
};
7484

7585
public static TheoryData<string, string[]> NamespaceWithMultipleVersions => new()

src/Common/test/Common.Mvc.Tests/Conventions/VersionByNamespaceConventionTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public partial class VersionByNamespaceConventionTest
2424
{ "Contoso.Api.v2018_04_01_1_0_Beta.Controllers", "2018-04-01.1.0-Beta" },
2525
{ "MyRestaurant.Vegetarian.Food.v1_1.Controllers", "1.1" },
2626
{ "VersioningSample.V5.Controllers", "5.0" },
27+
{ "_1", "1.0" },
28+
{ "_1_1", "1.1" },
29+
{ "_20180401", "2018-04-01" },
30+
{ "_2018_04_01", "2018-04-01" },
31+
{ "_2018_04_01_Beta", "2018-04-01-Beta" },
32+
{ "_2018_04_01_1_0_Beta", "2018-04-01.1.0-Beta" },
33+
{ "Contoso.Api._2018_04_01.Controllers", "2018-04-01" },
34+
{ "Contoso.Api._1.Controllers", "1.0" },
35+
{ "Contoso.Api._1_1.Controllers", "1.1" },
36+
{ "Contoso.Api._0_9_Beta.Controllers", "0.9-Beta" },
2737
};
2838

2939
private sealed class TestType : TypeDelegator

0 commit comments

Comments
 (0)