Open Closed

Service Methods with Optional Parameters named Id get added to the Path #8043


User avatar
0
BenS-FreshRF created
  • ABP Framework version: v8.3.0
  • UI Type: Blazor WASM
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue: Create a service method with a parameter named whateverId with a default value making it optional

Convention is dictating that any service methods with an *Id parameter get added to to the path for that service, i.e. /api/app/widget/fetch/{*Id}

However if that parameter is optional and provided a default value it is still added to the path, i.e. /api/app/widget/fetch/{typeId=null}

Intention is to use that optional *Id parameter as a filter, however because it is added to the path it becomes required (even Swagger stating as such).

The ABP service injected into the Client doesn't recognise this however, so calling one of these methods and not providing the optional parameter causes it to fail to find the correct route. But this doesn't cause an error, instead as an async method it just returns a Task that never completes, causing the method calling it to hang.

Workaround is to replace the parameters with a proper filter DTO, although renaming the parameter probably also works (I didn't test that however).


4 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello ,

    In your scenario, the endpoint /api/app/books/{id} requires an id, meaning the id is mandatory. If you don't provide an id, an error will be shown.

    However, you want to pass the id without showing it in the path, and still retrive the record whether or not the id is provided. To achieve this, you should use the endpoint /api/app/books and pass the id as a filter parameter. This way, it will work correctly and show the record regardless of whether the id is passed or not.

    e.g :-

    Thank you.

  • User Avatar
    0
    BenS-FreshRF created

    In your example none of your parameters are named *Id.

    Creating an ABP service with the following setup triggers the bug:

    public interface IBookAppService : IApplicationService {
        Task<string> FetchBooks(int? bookGenreId = null);
    }
    

    And that generates the following route as shown in Swagger:

    The nullable parameter with a default value is now a required parameter and a part of the path.

    If you then inject that service and call FetchBooks using the default value the result just hangs, no errors are thrown.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Thanks. We will fix this in the next version.

    Using another parameter name will avoid this situation.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    https://github.com/abpframework/abp/issues/21009

Made with ❤️ on ABP v9.1.0-preview. Updated on October 22, 2024, 09:35