Search code examples
c#visual-studio-2019autorest

AutoRest generating nullable boolean for method return type


I have problem with generat RestApiClient for CSharp. Problem was discovered in Visual Studio AutoRest so i download AutoRest CLI but is generate same wrong method return type.

AutoRest code generation utility [cli version: 3.0.6187; node: v12.14.1, max-memory: 8192 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest


Showing All Installed Extensions

 Type       Extension Name                           Version 
 core       @autorest/core                           3.0.6274     
 core       @microsoft.azure/autorest-core           2.0.4417     
 extension  @microsoft.azure/autorest.csharp         2.3.84      
 extension  @microsoft.azure/autorest.modeler        2.3.55       

This is Swagger.json generated by Swashbucke 5.6.0 from WebApi

{
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "Server"
    },
    "host": "localhost:5992",
    "schemes": [
        "http"
    ],
    "paths": {
        "/api/User/HasUser": {
            "post": {
                "tags": [
                    "User"
                ],
                "operationId": "User_HasUser",
                "consumes": [],
                "produces": [
                    "application/json",
                    "text/json",
                    "application/xml",
                    "text/xml"
                ],
                "parameters": [
                    {
                        "name": "username",
                        "in": "query",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                            "schema": {
                              "x-nullable": false,
                              "type": "boolean"
                            }
                    }
                }
            }
        }
    },
    "definitions": {}
}

command with parameters

c:\> autorest --input-file=swagger.json --output-folder=autorest --csharp --clear-output-folder

Output of AutoRest

namespace Api
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Rest;

    public static partial class UserExtensions
    {
            public static bool? HasUser(this IUser operations, string username) ...    
    }
}

I am expecting "bool" not "bool?"

public static bool HasUser(this IUser operations, string username) ...    

Solution

  • According to this issue on autorest GitHub: x-nullable not working for response primitives, there is an issue with the autorest generators version 2 that they do not handle x-nullable for primitive types.

    And since there is now a new version (3) of the generators, this will not be fixed. Could you update autorest and try with the new generators?

    Update 2020-05-11:

    I made a small test (should have done that before answering, sorry about that) and support for x-nullable on primitive return types does not seem to have been implemented yet. So with autorest, you may be out of luck.

    I tried your swagger.json with nswag:

    nswag openapi2csclient /input:swagger.json /classname:UserApiClient /namespace:UserApi /output:UserApiClient.cs
    

    and it generates a method with correct return type:

    public System.Threading.Tasks.Task<bool> HasUserAsync(string username)