Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI 3: Generate deepObject for complex query parameter #1594

Open
RicoSuter opened this issue Sep 11, 2018 · 3 comments
Open

OpenAPI 3: Generate deepObject for complex query parameter #1594

RicoSuter opened this issue Sep 11, 2018 · 3 comments

Comments

@RicoSuter
Copy link
Owner

Sample: #1591

@khenzar
Copy link

khenzar commented May 13, 2020

This is a much needed feature.

Trying to adopt CQRS pattern, which would greatly simplify API controllers, requires a use of complex types in action queries.

Example (Using ASP.NET Core 3.1).
My controller action:

[ApiController]
[Route("api/[controller]")]
[ApiConventionType(typeof(DefaultApiConventions))]
public class WorkflowsController : ControllerBase
{
  ...
  [HttpGet(nameof(GetWorkflow))]
  public async Task<ActionResult<Workflow>> GetWorkflow([FromQuery] GetWorkflowQuery query)
    {
           var workflow = await Mediator.Send(query);
           return Ok(workflow);
    }
}

Query object:

using MediatR;
public class GetWorkflowQuery : IRequest<Workflow>
    {
        public Guid Id { get; set; }
        public EntityEnvironment Environment { get; set; }
        public bool IncludeOperations { get; set; }
        public bool IncludeDataPaths { get; set; }
        public bool AsNoTracking { get; set; }
    }

public enum EntityEnvironment
    {
        Staging,
        Production,
        Backup
    }

Generated swagger spec:

"/api/Workflows/GetWorkflow": {
      "get": {
        "tags": [
          "Workflows"
        ],
        "operationId": "Workflows_GetWorkflow",
        "requestBody": {
          "x-name": "query",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/GetWorkflowQuery"
              }
            }
          },
          "required": true,
          "x-position": 1
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Workflow"
                }
              }
            }
          }
        }
      }
    },
....
"GetWorkflowQuery": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string",
            "format": "guid"
          },
          "environment": {
            "$ref": "#/components/schemas/EntityEnvironment"
          },
          "includeOperations": {
            "type": "boolean"
          },
          "includeDataPaths": {
            "type": "boolean"
          },
          "asNoTracking": {
            "type": "boolean"
          }
        }
      },

Generation API Documentation:
image

Error when trying to execute the action:
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

Error from generated C# client calling the action:

System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
   at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.Http.HttpClientHandler.StartGettingRequestStream(RequestState state)
   at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at ...API.Clients.WorkflowsClient.<GetWorkflowAsync>d__17.MoveNext()

@RicoSuter
Copy link
Owner Author

Is this with the aspnetcore generator?

@bleish
Copy link

bleish commented Feb 27, 2023

We're looking for a new c# client generator and want to use NSwag, but this feature is required for our API and preventing us from moving over. Has there been any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants