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

All "result" types should take an IEnumerable<T> as input instead of List<T>. #99

Closed
egil opened this issue Mar 19, 2021 · 0 comments · Fixed by #100
Closed

All "result" types should take an IEnumerable<T> as input instead of List<T>. #99

egil opened this issue Mar 19, 2021 · 0 comments · Fixed by #100
Assignees
Labels
enhancement New feature or request

Comments

@egil
Copy link
Contributor

egil commented Mar 19, 2021

This spec:

openapi: 3.0.0
paths:
  /:
    get:
      summary: Get
      description: Get
      operationId: get
      tags: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string

should result in the following C# (namespace and preamble excluded for brevity):

public class GetResult : ResultBase
{
    private GetResult(ActionResult result) : base(result) { }

    /// <summary>
    /// 200 - Ok response.
    /// </summary>
    public static GetResult Ok(IEnumerable<string> response)
        => new GetResult(new OkObjectResult(response ?? Enumerable.Empty<string>()));
}

I am considering removing the implicit conversion method, since C# doesnt allow implicit conversion from an interface to a concrete type. The alternative is to keep it, but define it as List<T>, e.g.:

public class GetResult : ResultBase
{
    private GetResult(ActionResult result) : base(result) { }

    /// <summary>
    /// 200 - Ok response.
    /// </summary>
    public static GetResult Ok(IEnumerable<string> response)
        => new GetResult(new OkObjectResult(response ?? Enumerable.Empty<string>()));

    /// <summary>
    /// Performs an implicit conversion from GetResult to ActionResult.
    /// </summary>
    public static implicit operator GetResult(List<string> response)
        => Ok(response);
}

Part of #93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant