dotnet / dotnet/aspnetcore

Incorrect OpenAPI schema generated for implicit services/special types/parseables

Open
#44,677 12 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I'm working on a talk I'll be giving soon on new features in ASP.NET Core 7 and Minimal APIs, and in the process of updating my [sample application](https://github.com/martincostello/aspnet-core-7-samples) for RC2 I've noticed a number of issues with the OpenAPI schema generated for various endpoints in the sample.

1. Using [implicit service resolution](https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-2/#infer-api-controller-action-parameters-that-come-from-services) for an [MVC controller](https://github.com/martincostello/aspnet-core-7-samples/blob/f0d3dccb4592afdd0dd49e984f3ef836158e1b69/src/TodoApp/SampleController.cs#L11-L16) renders the service as an object in the OpenAPI schema and as a query string parameter.

![image](https://user-images.githubusercontent.com/1439341/197166064-1eaaa438-3dd9-49b5-b85a-ad36cfffef4b.png)

![image](https://user-images.githubusercontent.com/1439341/197166119-40c0f46c-b7f7-4b44-a4f8-d831b8240b8b.png)

2. Using [TryParse() support](https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-3/#bind-using-tryparse-in-mvc-and-api-controllers) for parameters for an [MVC controller](https://github.com/martincostello/aspnet-core-7-samples/blob/f0d3dccb4592afdd0dd49e984f3ef836158e1b69/src/TodoApp/SampleController.cs#L21-L25) has a similar issue, and renders the query string parameter as an object mirroring the C# model, rather than as a string.

![image](https://user-images.githubusercontent.com/1439341/197166340-1291e735-3622-48c9-bf26-8785c8e10756.png)

![image](https://user-images.githubusercontent.com/1439341/197166384-d91d5370-f39b-47a6-8fd4-5edee8d7afa1.png)

3. Using the [support for `Stream` and `PipeReader`](https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-net-7-preview-1/#bind-the-request-body-as-a-stream-or-pipereader) to consume the request body shows both [`Stream`](https://github.com/martincostello/aspnet-core-7-samples/blob/f0d3dccb4592afdd0dd49e984f3ef836158e1b69/src/TodoApp/SampleEndpoints.cs#L171-L192) and [`PipeReader`](https://github.com/martincostello/aspnet-core-7-samples/blob/f0d3dccb4592afdd0dd49e984f3ef836158e1b69/src/TodoApp/SampleEndpoints.cs#L194-L201) in the schema.

![image](https://user-images.githubusercontent.com/1439341/197167499-1b61fa59-499e-49bf-8ca8-11577501f3f4.png)

![image](https://user-images.githubusercontent.com/1439341/197167543-34a6abef-257e-4e1e-a856-9c08add0b8ea.png)

### Expected Behavior

1. `MyService` is not shown as an endpoint parameter and is not included in the OpenAPI schemas.
2. The `name` parameter is shown as a simple string and `Name` is not included in the OpenAPI schemas.
3. `Stream` and `PipeReader` are not included in the OpenAPI schemas.

### Steps To Reproduce

1. Clone the [martincostello/aspnet-core-7-samples](https://github.com/martincostello/aspnet-core-7-samples) repository.
2. Build and run the application.
3. View the rendered OpenAPI schema with Swagger UI at `https://localhost:5001/swagger-ui/index.html`.

### Exceptions (if any)

None.

### .NET Version

7.0.100-rc.2.22477.23

### Anything else?

```sh
> dotnet --info
.NET SDK:
Version: 7.0.100-rc.2.22477.23
Commit: 0a5360315a

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100-rc.2.22477.23\

Host:
Version: 7.0.0-rc.2.22472.3
Architecture: x64
Commit: 550605cc93
```

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.