locked
Swagger HTTP POST - congestion with ALL navigation properties u => u.unworkable RRS feed

  • Question

  • User379720387 posted

    Attempting to add a record to entity Service.

    public partial class Service
        {
            public Service()
            {
                ProviderServices = new HashSet<ProviderService>();
            }
    
            [Key] public int ServiceId { get; set; }
            public string ServiceName { get; set; }
            public int? RoleId { get; set; }
            public bool? IsProcedure { get; set; }
            public string ShortForm { get; set; }
            public string Maincat { get; set; }
            public string Subcat { get; set; }
            public string Strength { get; set; }
            public string Package { get; set; }
            public string Size { get; set; }
            public string Note { get; set; }
    
            public virtual WebpagesRole Role { get; set; }
            public virtual ICollection<ProviderService> ProviderServices { get; set; }
        }

    The Swagger UI takes forever to populate because it wants to show all the related Entities that come with ProviderServices. It goes at least 8 Entities deep, times the number of users. It takes forever.

    Is there a way to tell Swagger that we are just updating 12 properties in Service and nothing else?

    [Route("AddNewService")]
            [HttpPost]
            public async Task<ActionResult<Service>> PostService(Service service)
            {
                context. Add(service);
                await context.SaveChangesAsync();
    
                return new CreatedAtRouteResult(nameof(GetServiceItem), new { id = service.ServiceId }, service);
            }

    Monday, March 1, 2021 9:30 PM

All replies

  • User-474980206 posted

    the proper way is to not return a Service (which appears to be a. Entity) but rather an response model that only has the supported properties.

    Tuesday, March 2, 2021 3:43 PM
  • User379720387 posted

    Yes, on Service being an Entity.

    Created a separate model called ServiceResponse

    public partial class ServiceResponse
        {
            
            [Key] public int ServiceId { get; set; }
            public string ServiceName { get; set; }
            public int RoleId { get; set; }
            public bool? IsProcedure { get; set; }
            public string ShortForm { get; set; }
            public string Maincat { get; set; }
            public string Subcat { get; set; }
            public string Strength { get; set; }
            public string Package { get; set; }
            public string Size { get; set; }
            public string Note { get; set; }
    
            
        }

    And the action, please tell me what goes where.

    [Route("AddNewService")]
            [HttpPost]
            public async Task<ActionResult<Service>> PostService(ServiceResponse serviceResponse)
            {
                try
                {
                    context.Add(serviceResponse);
                    await context.SaveChangesAsync();
                    return Ok(serviceResponse);
                }
                catch (Exception e)
                {
                    return BadRequest(e.Message);
                }
                //context. Add(service);
                //await context.SaveChangesAsync();
    
                //return new CreatedAtRouteResult(nameof(GetServiceItem), new { id = service.ServiceId }, service);
            }
    

    Tuesday, March 2, 2021 4:32 PM