I recently faced an issue in one of the APIs where as part of importing
OpenAPI to Azure APIM, it started throwing an error: "(ValidationError) Operation with the same method and URL template already
exists".
Let's have a look at a Minimal Reproducible Example (MRE).
Say I have the following ASP.NET Core API controller that is getting exposed
in OpenAPI Spec.
[Route("api/some/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long-resources")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public IEnumerable<string> Get()
{
return ["value1", "value2"];
}
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
When I import this to APIM, it's getting imported.
Note the operationId.
OpenAPI Specification |
Now let's remove the first endpoint and try to import again.
[Route("api/some/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long-resources")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
This time I am getting the error.
(ValidationError) Operation with the same method and URL template already exists |
We can fix this by adding operationId in the code, as follows.
[Route("api/some/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long-resources")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet("{id}", Name = "GetValueById")]
public string Get(int id)
{
return "value";
}
}
Explicit operationId |
Hope this helps.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment