OData is removing my existing parameters in Swagger

Viewed 267

I am trying to use Swagger with existing parameters. Adding in ODataQueryOptions removes All my other parameters in Swagger. How can I fix this to see my parameters? Code is below:

[HttpGet("[action]/{rollType}/{assessmentYear}/{lkAssessmentStatus}/{eventId?}")]
[ProducesResponseType(typeof(PageResult<BasicAssessmentDetailsDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(PageResult<BasicAssessmentDetailsDto>), StatusCodes.Status500InternalServerError)]
public ActionResult GetBasicPropertyAssessments(
    int rollType,
    int assessmentYear,
    int lkAssessmentStatus,
    int? eventId,
    ODataQueryOptions<BasicAssessmentDetailsDto> queryOptions)
{
    var result = assessmentRollService.GetBasicPropertyAssessmentsQueryable(rollType, assessmentYear, lkAssessmentStatus, eventId).ToODataPageResult(queryOptions);
    return Ok(result);
}

We added the following in Startup, and yet is still not working:

services.AddOData();
services.AddControllers().AddNewtonsoftJson();
services.AddMvc(options =>
{
    foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
    {
        outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }
    foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
    {
        inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
    }
}

Add the following to Configure method

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.EnableDependencyInjection();
    endpoints.Select().Expand().Filter().OrderBy().MaxTop(1000).Count();
});

namespace Common.OData
{
    /// <summary>
    /// This class allows us to add OData to our endpoints in swagger.
    /// </summary>
    public class ODataParametersSwaggerDefinition : IOperationFilter
    {
        private static readonly Type QueryableType = typeof(IQueryable);

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            var parameterDescriptions = context.ApiDescription.ParameterDescriptions;
            foreach (var parameter in parameterDescriptions)
            {
                // identify if any of the response types of the end point
                // return a queryable type.
                if (parameter.Type == typeof(ODataQueryOptions) || parameter.Type.IsSubclassOf(typeof(ODataQueryOptions)))
                {
                    // the schema of our new parameter (string)
                    var schema = new OpenApiSchema { Type = "string" };

                    operation.Parameters.Clear();

                    // $filter
                    operation.Parameters.Add(new OpenApiParameter
                    {
                        Name = "$filter",
                        Description = "Filter the results using OData syntax.",
                        Required = false, // these are all optional filters, so false.
                        In = ParameterLocation.Query, //specify to pass the parameter in the query
                        Schema = schema
                    });

                    // $orderby
                    operation.Parameters.Add(new OpenApiParameter
                    {
                        Name = "$orderby",
                        Description = "Order the results using OData syntax",
                        Required = false,
                        In = ParameterLocation.Query,
                        Schema = schema
                    });

                    // $skip
                    operation.Parameters.Add(new OpenApiParameter
                    {
                        Name = "$skip",
                        Description = "Skip the specified number of entries",
                        Required = false,
                        In = ParameterLocation.Query,
                        Schema = schema
                    });

                    // $top
                    operation.Parameters.Add(new OpenApiParameter
                    {
                        Name = "$top",
                        Description = "Get the top x number of records",
                        Required = false,
                        In = ParameterLocation.Query,
                        Schema = schema
                    });
                }
            }
        }

We do not see additional parameters

enter image description here

Currently using Net Core 3.1

0 Answers
Related