Skip to content

Reusable (Request/Response) Object #2122

@messerm

Description

@messerm

Hi,
we have reusable Request/Response Objects defined in our openApi 3 yaml file but the generated output doesn't seem to support this feature for request (and response) definition of a path.
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#components-object

Open API schema:

openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: "0.4.13"
  title: 'Test API'
################################################################
#
# PATHS
#
################################################################
paths:
  /projects:
    post:
      tags:
        - Project
      summary: 'Add project'
      operationId: addProject
      description: Add a project
      responses:
        '200':
          description: Succeeded
          content:
            application/json:
              schema:
                type: object
                required:
                  - name
                properties:
                  name:
                    type: string
      requestBody:
        $ref: '#/components/requestBodies/AddProjectRequestBody'

components:
  requestBodies:
    AddProjectRequestBody:
      description: Project to add
      content:
        application/json:
          schema:
            type: object
            required:
              - name
            properties:
              name:
                type: string
                description: name of the project
              color:
                type: number
                description: color as integer for the project

Take the following generation code:

var document = await SwaggerYamlDocument.FromFileAsync("openapi.yaml");

var settings = new SwaggerToCSharpClientGeneratorSettings();

var generator = new SwaggerToCSharpClientGenerator(document, settings);

var output = generator.GenerateFile(ClientGeneratorOutputType.Full);

await File.WriteAllTextAsync("Output.cs", output);

Output.cs contains the following code:

[System.CodeDom.Compiler.GeneratedCode("NSwag", "12.1.0.0 (NJsonSchema v9.13.28.0 (Newtonsoft.Json v9.0.0.0))")]
public partial interface IMyServiceClient
{
    /// <summary>Add project</summary>
    /// <returns>Succeeded</returns>
    /// <exception cref="MyServiceException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<Response> AddProjectAsync(object body);

    /// <summary>Add project</summary>
    /// <returns>Succeeded</returns>
    /// <exception cref="MyServiceException">A server side error occurred.</exception>
    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    System.Threading.Tasks.Task<Response> AddProjectAsync(object body, System.Threading.CancellationToken cancellationToken);
}

Defining the same using schemas works well:
Open API schema:

openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: "0.4.13"
  title: 'Test API'
################################################################
#
# PATHS
#
################################################################
paths:
  /projects:
    post:
      tags:
        - Project
      summary: 'Add project'
      operationId: addProject
      description: Add a project
      responses:
        '200':
          description: Succeeded
          content:
            application/json:
              schema:
                type: object
                required:
                  - name
                properties:
                  name:
                    type: string
      requestBody:
        description: Project to add
        content:
          application/json:
            schema:
               $ref: '#/components/schemas/AddProjectRequestBody'

components:
  schemas:
    AddProjectRequestBody:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          description: name of the project
        color:
          type: number
          description: color as integer for the project

output:

[System.CodeDom.Compiler.GeneratedCode("NSwag", "12.1.0.0 (NJsonSchema v9.13.28.0 (Newtonsoft.Json v9.0.0.0))")]
public partial interface IMyServiceClient
{
    /// <summary>Add project</summary>
    /// <param name="body">Project to add</param>
    /// <returns>Succeeded</returns>
    /// <exception cref="MyServiceException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<Response> AddProjectAsync(AddProjectRequestBody body);

    /// <summary>Add project</summary>
    /// <param name="body">Project to add</param>
    /// <returns>Succeeded</returns>
    /// <exception cref="MyServiceException">A server side error occurred.</exception>
    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    System.Threading.Tasks.Task<Response> AddProjectAsync(AddProjectRequestBody body, System.Threading.CancellationToken cancellationToken);
}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.13.28.0 (Newtonsoft.Json v9.0.0.0)")]
public partial class AddProjectRequestBody 
{
    /// <summary>name of the project</summary>
    [Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.Always)]
    [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
    public string Name { get; set; }

    /// <summary>color as integer for the project</summary>
    [Newtonsoft.Json.JsonProperty("color", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public double Color { get; set; }
}

Is this not supported or am I missing something? I can assume that this isn't an easy task since one can have different content types which would make generating the payload object more complicated.

Martyn

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions