RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.82k stars 1.3k forks source link

Default null handling not working as expected for .NET assembly #1523

Open jonocairns opened 6 years ago

jonocairns commented 6 years ago

Hi there!

Below is a screenshot of the setup in nswag studio.

image

(essentially setting "defaultReferenceTypeNullHandling": "NotNull" in the nswag file)

    public class BrandProps
    {
        public Brand Brand { get; set; }
        public string FieldName { get; set; }
        public bool IsEditable { get; set; }
        public string ErrorMessage { get; set; }
    }

Is exporting to this

export interface BrandProps {
    brand?: Brand | undefined;
    fieldName?: string | undefined;
    isEditable?: boolean | undefined;
    errorMessage?: string | undefined;
}

I would expect it to be something like this

export interface BrandProps {
    brand: Brand;
    fieldName: string;
    isEditable: boolean;
    errorMessage: string;
}

Is there something I'm not doing right? Thanks in advance!

RicoSuter commented 6 years ago

Can you also post the generated swagger spec?

jonocairns commented 6 years ago
{
  "runtime": "Default",
  "defaultVariables": null,
  "swaggerGenerator": {
    "typesToSwagger": {
      "classNames": [
        "admin.Models.BrandProps"
      ],
      "defaultPropertyNameHandling": "CamelCase",
      "defaultReferenceTypeNullHandling": "NotNull",
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "generateKnownTypes": true,
      "generateXmlObjects": false,
      "output": null,
      "outputType": "Swagger2",
      "assemblyPaths": [
        "../admin/bin/admin.dll"
      ],
      "assemblyConfig": null,
      "referencePaths": []
    }
  },
  "codeGenerators": {
    "swaggerToTypeScriptClient": {
      "className": "{controller}Client",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.4,
      "template": "Fetch",
      "promiseType": "Promise",
      "httpClass": "Http",
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "rxJsVersion": 5.0,
      "dateTimeType": "Date",
      "nullValue": "Undefined",
      "generateClientClasses": false,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "wrapDtoExceptions": false,
      "clientBaseClass": null,
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": null,
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": false,
      "generateCloneMethod": false,
      "typeStyle": "Interface",
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": null,
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "../admin/Scripts/bundles/models/index.ts"
    }
  }
}
jonocairns commented 6 years ago

Also worth noting this only happens when we change the TS version from 1.8 to 2.4

RicoSuter commented 6 years ago

This is the NSwag config... Can you post the relevant (properties) part of the generated spec?

image

jonocairns commented 6 years ago

sorry!

{
  "swagger": "2.0",
  "info": {
    "title": "",
    "version": ""
  },
  "definitions": {
    "BrandProps": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "brand": {
          "$ref": "#/definitions/Brand"
        },
        "fieldName": {
          "type": [
            "null",
            "string"
          ]
        },
        "isEditable": {
          "type": "boolean"
        },
        "errorMessage": {
          "type": [
            "null",
            "string"
          ]
        }
      }
    },
    "Brand": {
      "type": "integer",
      "description": "",
      "x-enumFlags": true,
      "x-enumNames": [
        "MyFoodBag",
        "BargainBox",
        "FreshStart"
      ],
      "enum": [
        1,
        2,
        4
      ]
    }
  }
}
RicoSuter commented 6 years ago

This:

          "type": [
            "null",
            "string"
          ]

looks like its generating for JSON Schema and is not supported in Swagger/OpenAPI... must be a problem with the generator in NJsonSchema... need to investigate/reproduce with a unit test there.

srollinet commented 6 years ago

I also have an issue with the config "defaultReferenceTypeNullHandling": "NotNull" Version 11.18.4 is ok, but in version 11.18.5, all the query parameters are not required anymore

        [HttpPut("{id}/camera")]
        public Task AffectCamera(string id, string cameraId)
        {
        }

left is 11.18.4 and right is 11.18.5 image

RicoSuter commented 6 years ago

I think this commit is the problem: https://github.com/RSuter/NSwag/commit/dc16d71e9ac1d459e10032cdfd3ab00765438d1e

RicoSuter commented 6 years ago

defaultReferenceTypeNullHandling does NOT control how parameters are handled only DTO properties... can you create a new issue with this problem?

srollinet commented 6 years ago

I opened issue #1556