Skip to content

OpenAPI: OperationId not set when EndpointName attribute is not available #59053

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

Closed
1 task done
thoekstraorama opened this issue Nov 19, 2024 · 2 comments
Closed
1 task done
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@thoekstraorama
Copy link

thoekstraorama commented Nov 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When generating an openapi.json using Microsoft.AspNetCore.OpenApi and Microsoft.Extensions.ApiDescription.Server the OperationId will not be set automically when the EndpointName attribute is not present.

openapi.json is based on controllers in AspNetCore.

Expected Behavior

Expected that OperationId is set following bullet points below. OperationId would be generating following third bullet point.

#54598

  • If a name is provided on an action via the route method, use that name.
  • If a name is provided on an action or endpoint using the EndpointName metadata, use that name.
  • If neither is available, attempt to create an operation ID using information available in the route template and endpoint metadata in sequential order.
    o The HTTP method associated with the operation stringified (GET, POST, etc.)
    o If route segments exist on the application, the route segment values concatenated by _.
    o If the route segments contain parameters, use the parameter name sans any constraints.

Steps To Reproduce

Reproduce using following repository:
https://github.com/hoekstrit/microsoft.openapi.operationid

After building the project/solution it will result in following openapi.json

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Nov 19, 2024
@captainsafia
Copy link
Member

@hoekstrit Thanks for filing this issue!

The behavior that you've referenced was proposed behavior outlined in the original issue but didn't land in the final implementation.

The most accurate guidance on how to set various properties is available on this docs page).

@captainsafia captainsafia added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Nov 19, 2024
@thoekstraorama
Copy link
Author

thoekstraorama commented Nov 20, 2024

Thank you very much for you answer. I do think that the documentation is missing another option to set the operationId. If I'm correct it's possible to set the operationId as well by setting the name on the HttpMethodAttribute.

Controller

[HttpGet(Name = "GetWeatherForecasts")]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

openapi.json

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApi.Api | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/WeatherForecast": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "operationId": "GetWeatherForecasts",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32"
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "WeatherForecast"
    }
  ]
}

I'll close this issue for now as I'm happy with the provided answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants