Skip to content

Fixed: Produce error on invalid ID in request body #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2024
Merged

Conversation

bkoelman
Copy link
Member

@bkoelman bkoelman commented Jul 13, 2024

This PR fixes the bug where an empty string or whitespace for id is incorrectly accepted in a request body when TId is not string.
The behavior of RuntimeTypeConverter remains unchanged (due to potential impact). The check is implemented in the deserialization of incoming request bodies.

Test coverage for the three modes of ClientIdGenerationMode has improved. Tests for all endpoints using string as TId were added. These use a single space, because an empty string results in a broken experience: you can't get-by-id, delete, or update such a resource, and rendered links are unusable.

Additionally, this PR makes a type's default value lookup 20% faster and reduces allocations by 90% (see commit message for details).

Fixes #1485.

QUALITY CHECKLIST

Copy link

codecov bot commented Jul 13, 2024

Codecov Report

Attention: Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.

Project coverage is 90.85%. Comparing base (28e8917) to head (bce1d8f).

Files Patch % Lines
...nApiDotNetCore/Resources/IdentifiableExtensions.cs 25.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1593      +/-   ##
==========================================
+ Coverage   90.80%   90.85%   +0.04%     
==========================================
  Files         348      348              
  Lines       11185    11189       +4     
  Branches     1836     1838       +2     
==========================================
+ Hits        10157    10166       +9     
+ Misses        676      672       -4     
+ Partials      352      351       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

bkoelman added 3 commits July 13, 2024 23:38
| Method                  | Mean     | Error    | StdDev   | Ratio | Allocated | Alloc Ratio |
|------------------------ |---------:|---------:|---------:|------:|----------:|------------:|
| ActivatorCreateInstance | 84.13 ms | 4.029 ms | 1.046 ms |  1.00 |      69 B |        1.00 |
| LookupCached            | 67.93 ms | 0.345 ms | 0.053 ms |  0.81 |       7 B |        0.10 |

```c#
using System.Collections.Concurrent;
using BenchmarkDotNet.Attributes;

namespace Benchmarks;

// ReSharper disable once ClassCanBeSealed.Global
[MarkdownExporter]
[SimpleJob(1, 5, 5)]
[MemoryDiagnoser]
public class DefaultValueBenchmarks
{
    private const int IterationCount = 10_000_000;

    private static readonly ConcurrentDictionary<Type, object?> Cache = new()
    {
        [typeof(int?)] = null,
        [typeof(Guid?)] = null
    };

    [Benchmark(Baseline = true)]
    public void ActivatorCreateInstance()
    {
        for (int index = 0; index < IterationCount; index++)
        {
            _ = Activator.CreateInstance(typeof(int?));
            _ = Activator.CreateInstance(typeof(Guid?));
        }
    }

    [Benchmark]
    public void LookupCached()
    {
        for (int index = 0; index < IterationCount; index++)
        {
            _ = Cache.TryGetValue(typeof(int?), out _);
            _ = Cache.TryGetValue(typeof(Guid?), out _);
        }
    }
}
```
@bkoelman bkoelman marked this pull request as ready for review July 14, 2024 00:45
@bkoelman bkoelman merged commit bc22ed1 into master Jul 14, 2024
16 checks passed
@bkoelman bkoelman deleted the fix-empty-id branch July 14, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Empty string ID is accepted in post resource request
1 participant