AdaskoTheBeAsT / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
66 stars 13 forks source link

Non Nullable Reference Type Property Default value should not be null #22

Closed luizfbicalho closed 9 months ago

luizfbicalho commented 1 year ago

If a property is not nullable, does the Property.Type.Default() should never return null right?

I had to change this code in my template ts public $Name: $Type $IsNullableType = $ReferenceDefaultValue;

    string IsNullableType(Property prop)
    {
        if(prop.Type.IsNullable)
        {
            return "| null";
        }
        return "";

    }
    string ReferenceDefaultValue(Property prop)
    {

        if(prop.Type.IsNullable)
        {
            return "null";
        }
        var propName = prop.Type.Name;
        if( propName == "string")
        {
        return "\"\"";
        }
        if( propName == "Date")
        {
            return "new Date()";
        }
        return "new " + propName + "()";
        //return  prop.Type.Default();
    }

This is a reference to PR #1

should this be inside the type ?

AdaskoTheBeAsT commented 1 year ago

I will check on weekend - thanks for spotting this

luizfbicalho commented 1 year ago

@AdaskoTheBeAsT Is there anything I can help you with?

In the original project I added some issues that never worked, would like to help with this

AdaskoTheBeAsT commented 1 year ago

@luizfbicalho you can test it https://github.com/AdaskoTheBeAsT/Typewriter/releases/tag/v2.12.0

AdaskoTheBeAsT commented 1 year ago

this is sample generated https://github.com/AdaskoTheBeAsT/NetCoreTypewriterRecipes/blob/master/src/AngularWebApiSample/ClientApp/apps/client-app/src/api/models/CombinedQueryModel.ts

luizfbicalho commented 1 year ago

It worked kind of fine on one place, But I got a lot of errors in other places,

for example in some places I need the string | null , but some times I need only the string, is there a new property that I can rely on?

I have a lot of code in tst tat I would like to make an extension or something like just a include , do you have any plans on that?

I think that the method.Url() changed, from this

api/ControllerBase/?actionName=${encodeURIComponent(actionName)}

to this

api/ControllerBase/?actionName=${actionName}

AdaskoTheBeAsT commented 1 year ago

I will check uri generated - for other things i will need sample class - rule is simple - whenever in c# somwthing is of type string? - it generates string | null in TypeScript

luizfbicalho commented 1 year ago

Thanks

I just found this problem in Url, the others I had to create a function to replace the " | null" in the type.Name I think that you could create a property StrictName or other name, that didn't have the | null

One thing in parallel, what should be the process to add one dll with extensions to the typewriter, like the WebApi Extension?

AdaskoTheBeAsT commented 1 year ago

ok I fixed regression with encodeUriComponent for security reason I do not accept dll's only code in pull requests

I can provide only flag for global either adding | null for all types or not custom treating every class can be realised by devs by using custom attributes as I have shown in case of enums

luizfbicalho commented 1 year ago

Thanks, I'll try the encode

I think that I can't explain my problem and I'm creating a confusion, so I'll create new issues if it's the case

Instead of this code, what do you suggest

 var propName = prop.Type.Name.Replace("[]","").Replace(" | null","");
     if( propName != "string" 
      && propName != "number" 
      && propName != "Date"
      && propName != "boolean"
      && propName != "any"
      && propName != "void"
      && !prop.Type.IsEnum)
     {