Skip to content

Commit 838cdea

Browse files
authored
Merge pull request #149 from azhoshkin/master
Added correct usage of CanRead/CanReadType.
2 parents 8a5cad2 + 06cd3a3 commit 838cdea

File tree

7 files changed

+519
-517
lines changed

7 files changed

+519
-517
lines changed

src/WebApiContrib.Core.Formatter.Csv/CsvInputFormatter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterCo
4545
return InputFormatterResult.SuccessAsync(result);
4646
}
4747

48-
public override bool CanRead(InputFormatterContext context)
48+
protected override bool CanReadType(Type type)
4949
{
50-
var type = context.ModelType;
5150
if (type == null)
5251
throw new ArgumentNullException("type");
5352

@@ -127,7 +126,7 @@ private object ReadStream(Type type, Stream stream)
127126
}
128127
return array;
129128
}
130-
129+
131130
return list;
132131
}
133132
}

src/WebApiContrib.Core.Formatter.MessagePack/MessagePackInputFormatter.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,14 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
4949
return formatterResult;
5050
}
5151

52-
public override bool CanRead(InputFormatterContext context)
52+
protected override bool CanReadType(Type type)
5353
{
54-
if (context == null)
55-
{
56-
throw new ArgumentNullException(nameof(context));
57-
}
58-
59-
if (context.ModelType == null)
54+
if (type == null)
6055
{
61-
throw new ArgumentException("Model Type cannnot be null");
56+
throw new ArgumentException("Type cannot be null");
6257
}
6358

64-
var typeInfo = context.ModelType.GetTypeInfo();
59+
var typeInfo = type.GetTypeInfo();
6560
return !typeInfo.IsAbstract && !typeInfo.IsInterface && typeInfo.IsPublic;
6661
}
6762
}

src/WebApiContrib.Core.Formatter.Protobuf/ProtobufInputFormatter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace WebApiContrib.Core.Formatter.Protobuf
88
{
99
public class ProtobufInputFormatter : InputFormatter
1010
{
11-
private readonly ProtobufFormatterOptions _options;
12-
13-
public ProtobufInputFormatter(ProtobufFormatterOptions protobufFormatterOptions)
14-
{
15-
_options = protobufFormatterOptions ?? throw new ArgumentNullException(nameof(protobufFormatterOptions));
16-
foreach (var contentType in protobufFormatterOptions.SupportedContentTypes)
17-
{
18-
SupportedMediaTypes.Add(new MediaTypeHeaderValue(contentType));
11+
private readonly ProtobufFormatterOptions _options;
12+
13+
public ProtobufInputFormatter(ProtobufFormatterOptions protobufFormatterOptions)
14+
{
15+
_options = protobufFormatterOptions ?? throw new ArgumentNullException(nameof(protobufFormatterOptions));
16+
foreach (var contentType in protobufFormatterOptions.SupportedContentTypes)
17+
{
18+
SupportedMediaTypes.Add(new MediaTypeHeaderValue(contentType));
1919
}
2020
}
2121

@@ -38,7 +38,7 @@ public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterCo
3838
return InputFormatterResult.SuccessAsync(result);
3939
}
4040

41-
public override bool CanRead(InputFormatterContext context)
41+
protected override bool CanReadType(Type type)
4242
{
4343
return true;
4444
}

src/WebApiContrib.Core.Formatter.Yaml/YamlInputFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterCo
3232
}
3333
}
3434

35-
public override bool CanRead(InputFormatterContext context)
35+
protected override bool CanReadType(Type type)
3636
{
3737
return true;
3838
}

tests/WebApiContrib.Core.MessagePack.Tests/MessagePackTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public async Task Post_MessagePack_Header()
7373
};
7474

7575
request.Content = new ByteArrayContent(MessagePackSerializer.Serialize<Book>(book, ContractlessStandardResolver.Instance));
76+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-msgpack");
7677
var result = await client.SendAsync(request);
7778

7879
var echo = MessagePackSerializer.Deserialize<Book>(await result.Content.ReadAsStreamAsync(), ContractlessStandardResolver.Instance);

0 commit comments

Comments
 (0)