AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Swagger - Multiple blocks are expanded simultaneously

Open vladimirlashko opened this issue 3 years ago • 14 comments

Assemblies affected ASP.NET Core OData 8.x

Describe the bug When you click on the operation (block) in swagger, multiple blocks expanded instead of one.

Reproduce steps

  1. Run your sample ODataRoutingSample
  2. Go to http://localhost:64771/swagger
  3. Click on "GET ​/v1​/Customers" block Expected result: only GET block expanded Actual result: "POST ​/v1​/Customers" expanded too.

Screenshots image

Additional info This happens because the same operationId generated in the swagger.json:

"/v1/Customers": { "get": { "tags": [ "Customers" ], "operationId": "v1/Customers", "responses": { "200": { "description": "Success" } } }, "post": { "tags": [ "Customers" ], "operationId": "v1/Customers", <<< must be unique

vladimirlashko avatar Aug 08 '22 06:08 vladimirlashko

I don't see this being an AspNetCoreOData issue. Can you elaborate why you think it is?

Also, are you using OpenAPI v3 output on swagger?

julealgon avatar Aug 08 '22 14:08 julealgon

I don't know who calculate operationId actually. I reproduced the issue on ASP.NET Core OData sample.

I also found a workaround with CustomOperationIds here. Maybe it should be a part of ASP.NET Core OData?

Also, are you using OpenAPI v3 output on swagger?

Yes, the used OpenAPI version is 3.0.1

vladimirlashko avatar Aug 08 '22 14:08 vladimirlashko

@vladimirlashko Thanks for reporting this.

@julealgon Yes, You are right. I think it's not AspNetCore.OData issue. That's the swagger gen [Swashbuckle.AspNetCore] issue (Or a configuration problem). @vladimirlashko You'd better to follow up swagger gen to update the operationId?

In the sample, I also have another swagger gen. It's from this repo: https://github.com/microsoft/OpenAPI.NET.OData . (Unfortunately, that's also my original work. :)

If based on the format of operationId at here: https://github.com/microsoft/OpenAPI.NET.OData/blob/master/src/Microsoft.OpenApi.OData.Reader/Operation/EntitySetPostOperationHandler.cs#L48

the operationId should be unique.

But, SwashBulk doesn't know about OData. So, it simply uses the path item name as the operation Id.

xuzhg avatar Aug 09 '22 00:08 xuzhg

@xuzhg Do you recommend to make an issue at Swashbuckle.AspNetCore?

Btw, I tried to create a default ASP.NET Core Web API project and there is no issue because no operationId exists at the swagger.json: image

vladimirlashko avatar Aug 09 '22 07:08 vladimirlashko

@vladimirlashko I can see in one of my non-OData projects here that the operationID is also not being generated.

When you tested the sample, was that sample using latest versions of all libraries? If so, something in the sample is causing these IDs to be generated. I'd assume the presence of OData in the project shouldn't affect that directly especially now that OData v8 uses standard routing.

Can you see if you spot anything suspicious between the sample and your project? Can you add OData to your project and check if that by itself starts generating operationIDs?

julealgon avatar Aug 09 '22 13:08 julealgon

@julealgon The sample resided in the AspNetCoreOData repository (this repository). I use the latest version of the sources. I also updated Microsoft.OpenApi.OData and Swashbuckle.AspNetCore packages in this sample and the issue still reproduced on the sample.

I'd assume the presence of OData in the project shouldn't affect that directly especially now that OData v8 uses standard routing.

I think OData can affect OpenAPI cause it manipulates routing. E.g. in this code: https://github.com/OData/AspNetCoreOData/blob/main/sample/ODataRoutingSample/Controllers/ODataOpenApiController.cs#L39

Can you see if you spot anything suspicious between the sample and your project?

Nothing suspicious. Swashbuckle generates OperationIds with OData and does not generate without OData.

vladimirlashko avatar Aug 10 '22 07:08 vladimirlashko

@julealgon The sample resided in the AspNetCoreOData repository (this repository). I use the latest version of the sources. I also updated Microsoft.OpenApi.OData and Swashbuckle.AspNetCore packages in this sample and the issue still reproduced on the sample.

I'd assume the presence of OData in the project shouldn't affect that directly especially now that OData v8 uses standard routing.

I think OData can affect OpenAPI cause it manipulates routing. E.g. in this code: https://github.com/OData/AspNetCoreOData/blob/main/sample/ODataRoutingSample/Controllers/ODataOpenApiController.cs#L39

Can you see if you spot anything suspicious between the sample and your project?

Nothing suspicious. Swashbuckle generates OperationIds with OData and does not generate without OData.

But, you are sending " ~/swagger" not sending " ~/$openapi", right?

xuzhg avatar Aug 10 '22 18:08 xuzhg

@xuzhg Correct, I need " ~/swagger".

vladimirlashko avatar Aug 11 '22 05:08 vladimirlashko

@vladimirlashko has that solved the issue?

habbes avatar Aug 11 '22 09:08 habbes

@habbes What exactly? The issue is somewhere between ASP.NET Core OData and Swashbuckle. Please fix it OR please point me to the correct third-party repository so I will ask for fix there.

vladimirlashko avatar Aug 11 '22 09:08 vladimirlashko

Hello, any news here? Thanks!

vladimirlashko avatar Aug 22 '22 08:08 vladimirlashko

Probably a Swashbuckle issue. Try following:

// mvcBuilder of type IMvcBuilder.
mvcBuilder.Services.AddSwaggerGen(o =>
{
	// Provide unique operation ID.
	o.CustomOperationIds(x => string.Join("/", x.HttpMethod.ToLower(), x.RelativePath));
}

mgesing avatar Sep 21 '22 13:09 mgesing

I see this also. I click on an API operation in Swagger UI and the entry above it and the entry I selected are both expanded. This only happens for ODataController.

https://github.com/AJMauger/sunstealer.mvc.odata

AJMauger avatar Aug 12 '23 05:08 AJMauger

In Swagger 6.5.0 and OData 8.2.4 issue is still present, but workaround by specifying custom operation identifiers works well

Sergey-Terekhin avatar Apr 26 '24 07:04 Sergey-Terekhin