Skip to content

Commit

Permalink
Fix #667 - handle illegal response DTO instantiation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
KateyBee committed Apr 28, 2024
1 parent e050ea2 commit 5106bf6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Src/Directory.Build.props
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>

<Version>5.24.0.8-beta</Version>
<Version>5.24.0.9-beta</Version>

<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
Expand Down
15 changes: 11 additions & 4 deletions Src/Library/Endpoint/Endpoint.Properties.cs
Expand Up @@ -86,16 +86,23 @@ public bool ResponseStarted
}

static readonly JsonObject _emptyObject = new();
static readonly JsonArray _emptyArray = new();
static readonly JsonArray _emptyArray = [];

TResponse InitResponseDto()
{
if (_isStringResponse) //otherwise strings are detected as IEnumerable of chars
return default!;

_response = JsonSerializer.Deserialize<TResponse>(
_isCollectionResponse ? _emptyArray : _emptyObject,
Cfg.SerOpts.Options)!;
try
{
_response = JsonSerializer.Deserialize<TResponse>(
_isCollectionResponse ? _emptyArray : _emptyObject,
Cfg.SerOpts.Options)!;
}
catch
{
//do nothing
}

return _response is null
? throw new NotSupportedException(
Expand Down
15 changes: 15 additions & 0 deletions Src/Library/changelog.md
Expand Up @@ -110,6 +110,21 @@ There was a possible contention issue that could arise in and extremely niche ed

</details>

<details><summary>Correct exception not thrown when trying to instantiate response DTOs with required properties</summary>

When the response DTO contains required properties like this:

```csharp
public class MyResponse
{
public required string FullName { get; set; }
}
```

If an attempt was made to utilize the auto response sending feature by setting properties of the `Response` object, a 400 validation error was being thrown instead of a 500 internal server error. It is now correctly throwing the `NotSupportedException` as it should, because FE cannot automatically instantiate objects that have required properties and the correct usage is for you to instantiate the object yourself and set it to the `Response` property, which is what the exception will now be instructing you to do.

</details>

## Breaking Changes ⚠️

<details><summary>The way multiple Swagger request examples are set has been changed</summary>
Expand Down

0 comments on commit 5106bf6

Please sign in to comment.