Skip to content

[C#] Issues with float type in models #783

@albator1932

Description

@albator1932

It is always in model properties afaik (at least that's where I have the issue), for instance I do have this JSON generated in my API:

FunctionParameters:{  
   description:"Represents the parameters for a function.",
   required:[  
      "dummy"
   ],
   type:"object",
   properties:{  
      dummy:{  
         format:"float",
         description:"Dummy value for illustration.",
         default:15,
         type:"number"
      }
   }
}

And the generated C# result will be:

public FunctionParameters(float? dummy = 15.0) {
	// use default value if no "dummy" provided
	if (dummy == null)
	{
		this.dummy = 15.0;
	}
	else
	{
		this.dummy = dummy;
	}
}

which gives these errors:

Error	CS1750	A value of type 'double' cannot be used as a default parameter because there are no standard conversions to type 'float?'
Error	CS0266	Cannot implicitly convert type 'double' to 'float?'. An explicit conversion exists (are you missing a cast?)

The generated C# code should be:

public FunctionParameters(float? dummy = 15.0f) {
	// use default value if no "dummy" provided
	if (dummy == null)
	{
		this.dummy = 15.0f;
	}
	else
	{
		this.dummy = dummy;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions