Skip to content

OpenApi inline enum for query parameters #57979

Closed
@dnv-kimbell

Description

@dnv-kimbell

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have an operation with that takes in an enum as query string parameter, and returns it back in the response.

In the parameter section, the enum is just defined as string with no information about what valid values are

"parameters": [
	{
		"name": "e",
		"in": "query",
		"schema": {
			"type": "string"
		}
	}
],

In the response the enum is referenced and it's possible to figure out valid values

"responses": {
	"200": {
		"description": "OK",
		"content": {
			"application/json": {
				"schema": {
					"$ref": "#/components/schemas/EnumWithNoFlags"
				}
			}
		}
	}
}

Expected Behavior

The query parameter should use a reference to the enum schema.

Steps To Reproduce

https://github.com/dnv-kimbell/openapi-inlineschema

Exceptions (if any)

No response

.NET Version

9.0 RC1

Anything else?

No response

Activity

ghost added
old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
on Sep 20, 2024
added
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
and removed
old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
on Sep 21, 2024
martincostello

martincostello commented on Sep 21, 2024

@martincostello
Member

Just spotted this one myself with version 9.0.100-rc.2.24468.2 of the SDK and 9.0.0-rc.2.24470.14 of the M.A.OpenApi library.

With martincostello/openapi-extensions@5742a21 I get an empty enum for CarType:

"CarType": {
  "type": "integer",
  "description": "The type of the car."
}
captainsafia

captainsafia commented on Sep 22, 2024

@captainsafia
Member

The query parameter should use a reference to the enum schema.

The behavior that I find surprising here is that the schemas generated for the response and the query parameter are different. The schema generated by the query parameter uses a type: string with no enum list configured, whereas the schema configured for the response contains the correct enum listing. The difference in behavior between the two is the actual source of the issue.

Upon further debugging, the issue seemed to only manifest for endpoints that used controller actions (as opposed to minimal APIs) which is particularly strange given that the schema generation logic is shared between the two.

Some further debugging reveals that this might be an issue with the ApiExplorer layer for MVC because the ApiParameterDescription generates a default type of string for this argument:

image

Based on the logic here. My suspicion gravitated towards the recent changes we made to ModelMetadata to support native AoT since the referenced method interacts with the IsParsable and IsConvertibleType properties that we recently modified as a result of this work although that doesn't appear to be the case.

It seems like the actual root cause is some code that we added in 46b5580 that set the parameter type of simple types (including those that can be parsed from an enum) to string to support the introduction of TryParse binding support in MVC. Ultimately, that appears to be the issue.

Anyhow, I've figured out a bug fix for this that I hope to include in .NET 9 but figured I'd monologue a bit about the root case here for posterities sake.

self-assigned this
on Sep 22, 2024
added this to the 9.0.0 milestone on Sep 22, 2024
added a commit that references this issue on Sep 23, 2024
01bda26
added a commit that references this issue on Sep 25, 2024
029aaae
added a commit that references this issue on Sep 26, 2024
9896f43
captainsafia

captainsafia commented on Sep 26, 2024

@captainsafia
Member

@dnv-kimbell The fix for this has landed in nightly package version 9.0.0-rtm.24476.2.

You'll need to use the following PackageReference:

<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rtm.24476.2" />

And make sure that you have a reference to the nightly dotnet9 feed in your nuget.config file:

<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />

Can you let me know if that works for you?

added
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
on Sep 26, 2024
dnv-kimbell

dnv-kimbell commented on Sep 27, 2024

@dnv-kimbell
Author

When I try that package, I get a reference to the enum from the query section, both with and without flags, so it seems to be working as expected.

"parameters": [
	{
		"name": "e",
		"in": "query",
		"schema": {
			"$ref": "#/components/schemas/EnumWithFlags"
		}
	}
],

"parameters": [
	{
		"name": "e",
		"in": "query",
		"schema": {
			"$ref": "#/components/schemas/EnumWithNoFlags"
		}
	}
],
added
Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.
and removed
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
on Sep 27, 2024
captainsafia

captainsafia commented on Sep 27, 2024

@captainsafia
Member

@dnv-kimbell Great! Thanks for verifying. Closing this bug as fixed.

added a commit that references this issue on Dec 31, 2024
4ecc398
added a commit that references this issue on Feb 11, 2025
dc74341
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @martincostello@captainsafia@dnv-kimbell

      Issue actions

        OpenApi inline enum for query parameters · Issue #57979 · dotnet/aspnetcore