dotnet / dotnet/aspnetcore

XmlSerializer does not support positional records and that leads to misleading response status.

Open
#55,113 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I enabled `XmlFormatters` but I was not able to return a positional record when I specified `Accept: application/xlm`.

I've noticed the response contains the payload:
```

406
Not Acceptable
https://tools.ietf.org/html/rfc9110#section-15.5.7
00-104180fe893414bb679177f03cfd0373-0bb98bcd7fef683b-00

```

In the case of unsupported `Content-Type` it's usually just a response code.

Then I checked the output of the server and saw this exception:
> Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter: Warning: An error occurred while trying to create an XmlSerializer for the type 'ExceptionsTest.Controllers.MyDto'.

> System.InvalidOperationException: ExceptionsTest.Controllers.MyDto cannot be serialized because it does not have a parameterless constructor.
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)
Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Warning: No output formatter was found for content types 'application/xml' to write the response.

Here's a DTO:
```
public sealed record MyDto(string Property);
```

Here's the endpoint:
```
[HttpGet("get")]
public MyDto DoIt() => new MyDto(Property: "test");
```

### Expected Behavior

I expect this:
- `XmlSerializer` should provide support for positional records, as first .NET citizen,
- The framework should return 500 Internal Server Error in case the output (input?) formatter throws an exception when serializing. It's not the fault of the client. Or even better, there should be an option to verify conceptual (de)serializability of payloads for all active formatters or possibility to enable this validation per content-type.

- There should be an analyzer which emits a warning, if XmlFormatter is activated and payload contains positional records, in case support won't be provided.

- If it's going to be resolved as `won't fix|by design`, the behavior should be documented.

Here's the solution to reproduce the issue:
[ExceptionsTest.zip](https://github.com/dotnet/aspnetcore/files/14965939/ExceptionsTest.zip)

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

_No response_

### Anything else?

asp.net core 8.0.4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.