Skip to content

Commit 49c030a

Browse files
committed
minimal api 側からの openapi 対応
1 parent 15f83a1 commit 49c030a

24 files changed

Lines changed: 191 additions & 131 deletions

Directory.Build.targets

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@
1010
</ItemGroup>
1111
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net6.0'">
1212
<PackageReference Update="MSTest" Version="3.10.1" />
13-
<PackageReference Update="Microsoft.OpenApi" Version="1.6.28" />
1413
</ItemGroup>
1514
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
1615
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.14" />
17-
<PackageReference Update="Microsoft.AspNetCore.OpenApi" Version="9.0.14" />
1816
</ItemGroup>
1917
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
2018
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.25" />
21-
<PackageReference Update="Microsoft.AspNetCore.OpenApi" Version="8.0.25" />
2219
</ItemGroup>
2320
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
2421
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
25-
<PackageReference Update="Microsoft.AspNetCore.OpenApi" Version="7.0.20" />
2622
</ItemGroup>
2723
</Project>

Juner.AspNetCore.JsonSequence.code-workspace

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"**/Thumbs.db": true,
2727
"Sequence": true,
2828
"Sequence.Tests": true,
29+
"samples": true,
2930
},
3031
"explorer.fileNesting.enabled": true,
3132
"explorer.fileNesting.patterns": {

Sequence.Tests/Http/HttpResults/JsonSequenceResultTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void PopulateMetadataTest()
158158
// Assert
159159
var producesResponseTypeMetadata = builder.Metadata.OfType<IProducesResponseTypeMetadata>().Last();
160160
Assert.AreEqual(StatusCodes.Status200OK, producesResponseTypeMetadata.StatusCode);
161-
Assert.IsNull(producesResponseTypeMetadata.Type);
161+
Assert.AreEqual(typeof(Stream), producesResponseTypeMetadata.Type);
162162
var producesSequenceResponseTypeMetadata = builder.Metadata.OfType<IProducesSequenceResponseTypeMetadata>().Last();
163163
Assert.AreEqual(typeof(Todo), producesSequenceResponseTypeMetadata?.ItemType);
164164
var jsonSequence =

Sequence/Http/AcceptsSequenceMetadata.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ namespace Juner.AspNetCore.Sequence.Http;
55

66
public class AcceptsSequenceMetadata : IAcceptsSequenceMetadata, IAcceptsMetadata
77
{
8-
public AcceptsSequenceMetadata(Type? itemType = null, string[]? contentTypes = null, bool isOptional = false)
8+
public AcceptsSequenceMetadata(Type? itemType = null, IContent[]? contentTypes = null, bool isOptional = false)
99
{
1010
ItemType = itemType;
1111
IsOptional = isOptional;
1212

1313
if (contentTypes is null || contentTypes.Length == 0)
1414
{
1515
ContentTypes = [];
16+
OnlyContentTypes = [];
1617
}
1718
else
1819
{
1920
for (var i = 0; i < contentTypes.Length; i++)
2021
{
21-
MediaTypeHeaderValue.Parse(contentTypes[i]);
22-
ValidateContentType(contentTypes[i]);
22+
MediaTypeHeaderValue.Parse(contentTypes[i].ContentType);
23+
ValidateContentType(contentTypes[i].ContentType);
2324
}
2425

2526
ContentTypes = contentTypes;
27+
OnlyContentTypes = [.. contentTypes.Select(v => v.ContentType)];
2628
}
2729

2830
static void ValidateContentType(string type)
@@ -33,7 +35,12 @@ static void ValidateContentType(string type)
3335
}
3436
}
3537
}
36-
public IReadOnlyList<string> ContentTypes { get; init; }
38+
39+
public IReadOnlyList<IContent> ContentTypes { get; init; }
40+
41+
IReadOnlyList<string> IAcceptsMetadata.ContentTypes => OnlyContentTypes;
42+
43+
IReadOnlyList<string> OnlyContentTypes { get; init; }
3744

3845
public Type? RequestType => typeof(Stream);
3946

Sequence/Http/Content.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Juner.AspNetCore.Sequence.Http;
2+
3+
/// <summary>
4+
///
5+
/// </summary>
6+
/// <param name="ContentType"></param>
7+
/// <param name="IsStreaming"></param>
8+
public record Content(string ContentType, bool IsStreaming) : IContent;

Sequence/Http/HttpResults/JsonLineResultOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
6060
builder.Metadata.Add(new ProducesSequenceResponseTypeMetadata(
6161
STATUS_CODE,
6262
typeof(T),
63-
[CONTENT_TYPE]));
63+
[new Content(CONTENT_TYPE, true)]));
6464
}
6565
}

Sequence/Http/HttpResults/JsonSequenceResultOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
7070
builder.Metadata.Add(new ProducesSequenceResponseTypeMetadata(
7171
STATUS_CODE,
7272
typeof(T),
73-
[CONTENT_TYPE]));
73+
[new Content(CONTENT_TYPE, true)]));
7474
}
7575
}

Sequence/Http/HttpResults/NdJsonResultOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
6060
builder.Metadata.Add(new ProducesSequenceResponseTypeMetadata(
6161
STATUS_CODE,
6262
typeof(T),
63-
[CONTENT_TYPE]));
63+
[new Content(CONTENT_TYPE, true)]));
6464
}
6565
}

Sequence/Http/HttpResults/SequenceResultOfT.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
111111
builder.Metadata.Add(new ProducesSequenceResponseTypeMetadata(
112112
STATUS_CODE,
113113
typeof(T),
114-
[.. MakePatternList.Select(v => v.ContentType)]));
114+
[.. MakePatternList.Select(v => new Content(v.ContentType, v.IsStreaming))]));
115115
}
116116

117-
record MakePattern(string ContentType, ReadOnlyMemory<byte> Begin, ReadOnlyMemory<byte> End);
117+
record MakePattern(string ContentType, ReadOnlyMemory<byte> Begin, ReadOnlyMemory<byte> End, bool IsStreaming);
118118
static bool TryGetPattern(StringSegment contentType, out ReadOnlyMemory<byte> begin, out ReadOnlyMemory<byte> end)
119119
{
120120
begin = default;
121121
end = default;
122122
if (!MediaTypeHeaderValue.TryParse(contentType, out var parsedValue))
123123
return false;
124-
foreach (var (mediaType, begin2, end2) in MakePatternList)
124+
foreach (var (mediaType, begin2, end2, _) in MakePatternList)
125125
if (parsedValue.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase) == true)
126126
{
127127
(begin, end) = (begin2, end2);
@@ -175,22 +175,22 @@ static IEnumerable<MakePattern> MakePatterns()
175175
#else
176176
"application/json-seq";
177177
#endif
178-
yield return new(contentType, RS, LF);
178+
yield return new(contentType, RS, LF, true);
179179
}
180180
{
181181
const string contentType =
182182
"application/x-ndjson";
183-
yield return new(contentType, default, LF);
183+
yield return new(contentType, default, LF, true);
184184
}
185185
{
186186
const string contentType =
187187
"application/jsonl";
188-
yield return new(contentType, default, LF);
188+
yield return new(contentType, default, LF, true);
189189
}
190190
{
191191
const string contentType =
192192
"application/json";
193-
yield return new(contentType, default, default);
193+
yield return new(contentType, default, default, false);
194194
}
195195

196196
}

Sequence/Http/IAcceptsSequenceMetadata.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22

33
namespace Juner.AspNetCore.Sequence.Http;
44

5+
/// <summary>
6+
///
7+
/// </summary>
58
public interface IAcceptsSequenceMetadata : IAcceptsMetadata
69
{
10+
/// <summary>
11+
///
12+
/// </summary>
13+
new IReadOnlyList<IContent> ContentTypes { get; }
14+
15+
/// <summary>
16+
///
17+
/// </summary>
718
Type? ItemType { get; }
8-
}
19+
20+
}

0 commit comments

Comments
 (0)