Skip to content

Commit c0e4e6c

Browse files
committed
fix(inputFormatters): needed FromBody param attribute
1 parent 5f3370e commit c0e4e6c

File tree

6 files changed

+29
-47
lines changed

6 files changed

+29
-47
lines changed

.vscode/tasks.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
2-
// See https://go.microsoft.com/fwlink/?LinkId=733558
3-
// for the documentation about the tasks.json format
4-
"version": "0.1.0",
5-
"command": "dotnet",
6-
"isShellCommand": true,
7-
"args": [],
8-
"options": {
9-
"cwd": "${workspaceRoot}/src/Examples/JsonApiDotNetCoreExample"
10-
},
11-
"tasks": [
12-
{
13-
"taskName": "build",
14-
"args": [ ],
15-
"isBuildCommand": true,
16-
"showOutput": "silent",
17-
"problemMatcher": "$msCompile"
18-
}
19-
]
20-
}
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"command": "dotnet",
6+
"isShellCommand": true,
7+
"args": [],
8+
"options": {
9+
"cwd": "${workspaceRoot}"
10+
},
11+
"tasks": [
12+
{
13+
"taskName": "build",
14+
"args": [],
15+
"isBuildCommand": true,
16+
"showOutput": "silent",
17+
"problemMatcher": "$msCompile"
18+
}
19+
]
20+
}

src/Examples/OperationsExample/Startup.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using OperationsExample.Data;
1010
using JsonApiDotNetCore.Models;
11-
using JsonApiDotNetCore.Formatters;
1211

1312
namespace OperationsExample
1413
{
@@ -36,15 +35,10 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
3635
services.AddDbContext<AppDbContext>(options =>
3736
{
3837
options.UseNpgsql(GetDbConnectionString());
39-
}, ServiceLifetime .Transient);
38+
}, ServiceLifetime.Transient);
4039

4140
services.AddJsonApi<AppDbContext>(opt => opt.EnableExtension(JsonApiExtension.Operations));
4241

43-
services.AddMvc().AddMvcOptions(options => {
44-
options.InputFormatters.Clear();
45-
options.InputFormatters.Insert(0, new JsonApiOperationsInputFormatter());
46-
});
47-
4842
return services.BuildServiceProvider();
4943
}
5044

src/JsonApiDotNetCore/Controllers/JsonApiOperationsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public JsonApiOperationsController(IOperationsProcessor operationsProcessor)
1515
}
1616

1717
[HttpPatch]
18-
public async Task<IActionResult> PatchAsync(OperationsDocument doc)
18+
public async Task<IActionResult> PatchAsync([FromBody] OperationsDocument doc)
1919
{
2020
var results = await _operationsProcessor.ProcessAsync(doc.Operations);
2121
return Ok(results);

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public static void AddJsonApiInternals(
112112
services.AddScoped<IJsonApiWriter, JsonApiWriter>();
113113
services.AddScoped<IJsonApiDeSerializer, JsonApiDeSerializer>();
114114
services.AddScoped<IJsonApiReader, JsonApiReader>();
115+
services.AddScoped<IJsonApiOperationsReader, JsonApiOperationsReader>();
115116
services.AddScoped<IGenericProcessorFactory, GenericProcessorFactory>();
116117
services.AddScoped(typeof(GenericProcessor<>));
117118
services.AddScoped<IQueryAccessor, QueryAccessor>();
@@ -128,11 +129,11 @@ private static void AddOperationServices(IServiceCollection services)
128129

129130
public static void SerializeAsJsonApi(this MvcOptions options, JsonApiOptions jsonApiOptions)
130131
{
132+
options.InputFormatters.Insert(0, new JsonApiInputFormatter());
133+
131134
if (jsonApiOptions.EnabledExtensions.Contains(JsonApiExtension.Operations))
132135
options.InputFormatters.Insert(0, new JsonApiOperationsInputFormatter());
133136

134-
options.InputFormatters.Insert(0, new JsonApiInputFormatter());
135-
136137
options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
137138

138139
options.Conventions.Insert(0, new DasherizedRoutingConvention(jsonApiOptions.Namespace));

src/JsonApiDotNetCore/Formatters/JsonApiInputFormatter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ namespace JsonApiDotNetCore.Formatters
88
public class JsonApiInputFormatter : IInputFormatter
99
{
1010
public bool CanRead(InputFormatterContext context)
11-
{
11+
{
1212
if (context == null)
1313
throw new ArgumentNullException(nameof(context));
1414

1515
var contentTypeString = context.HttpContext.Request.ContentType;
1616

1717
var canRead = contentTypeString == "application/vnd.api+json";
1818

19-
Console.WriteLine($">>> JsonApiInputFormatter Can Read {canRead}");
20-
2119
return canRead;
2220
}
2321

2422
public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
2523
{
26-
Console.WriteLine($">>> JsonApiInputFormatter ReadAsync");
2724
var reader = context.HttpContext.RequestServices.GetService<IJsonApiReader>();
2825
return await reader.ReadAsync(context);
2926
}

src/JsonApiDotNetCore/Formatters/JsonApiOperationsInputFormatter.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
using System;
2-
using System.Text;
32
using System.Threading.Tasks;
43
using Microsoft.AspNetCore.Mvc.Formatters;
54
using Microsoft.Extensions.DependencyInjection;
65
using Microsoft.Extensions.Primitives;
7-
using Microsoft.Net.Http.Headers;
86

97
namespace JsonApiDotNetCore.Formatters
108
{
11-
public class JsonApiOperationsInputFormatter : TextInputFormatter
9+
public class JsonApiOperationsInputFormatter : IInputFormatter
1210
{
1311
const string PROFILE_EXTENSION = "<http://example.org/profiles/myjsonstuff>; rel=\"profile\"";
1412

15-
public JsonApiOperationsInputFormatter()
16-
{
17-
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/vnd.api+json"));
18-
SupportedEncodings.Add(Encoding.UTF8);
19-
SupportedEncodings.Add(Encoding.Unicode);
20-
}
21-
22-
public override bool CanRead(InputFormatterContext context)
13+
public bool CanRead(InputFormatterContext context)
2314
{
2415
if (context == null)
2516
throw new ArgumentNullException(nameof(context));
@@ -31,13 +22,12 @@ public override bool CanRead(InputFormatterContext context)
3122
context.HttpContext.Request.Headers.TryGetValue("Link", out StringValues profileExtension) &&
3223
profileExtension == PROFILE_EXTENSION
3324
);
34-
Console.WriteLine($">>> JsonApiOperationsInputFormatter Can Read {canRead}");
25+
3526
return canRead;
3627
}
3728

38-
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
29+
public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
3930
{
40-
Console.WriteLine($">>> JsonApiOperationsInputFormatter ReadAsync");
4131
var reader = context.HttpContext.RequestServices.GetService<IJsonApiOperationsReader>();
4232
return await reader.ReadAsync(context);
4333
}

0 commit comments

Comments
 (0)