openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] [ASPNETCORE] Add value types

Open evgenii-kuznetsov opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

Generator name: aspnetcore Generator cli version: 7.5.0

When generating a model with a UUID property, 'EmitDefaultValue' is set to 'false' if the field is required and not nullable. I think that in such case, it should be set to 'true'. For instance, an int property with the same configuration sets 'EmitDefaultValue' to 'true' because it is a value type. I believe that a Guid should also be considered a value type.

Describe the solution you'd like

I am wondering if it would be possible to extend the return values of the getValues function in AspNetServerCodegen.java to include "DateTime", "DateOnly", "DateTimeOffset", and "Guid". This suggestion is similar to the implementation in AbstractCSharpCodegen.java, where the code generator for C# client works as expected.

Describe alternatives you've considered

As a workaround, we have to override model.mustache by using x-csharp-value-type instead of x-is-value-type: [DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}{{#vendorExtensions.x-csharp-value-type}}true{{/vendorExtensions.x-csharp-value-type}}{{^vendorExtensions.x-csharp-value-type}}false{{/vendorExtensions.x-csharp-value-type}}{{/isNullable}})], but we would like to use the standard template as much as possible.

Additional context

Script:

docker run --rm -v $(pwd):/app openapitools/openapi-generator-cli:v7.5.0 generate \
    --input-spec /app/schema.yml \
    --output /app/src \
    --model-name-suffix Dto \
    --generator-name aspnetcore \
    --additional-properties aspnetCoreVersion=5.0 \
    --additional-properties nullableReferenceTypes=true \

Schema:

openapi: 3.0.1
info:
  title: Test
  version: 1.0.0

paths:
  /test-endpoint:
    get:
      responses:
        200:
          description: Success

components:
  schemas:
    TestModel:
      type: object
      required:
        - test_id
      properties:
        test_id:
          type: string
          format: uuid

Output:

[Required]
[DataMember(Name="test_id", EmitDefaultValue=false)]
public Guid TestId { get; set; }

evgenii-kuznetsov avatar May 02 '24 19:05 evgenii-kuznetsov

@wing328, I can create a PR for the proposed changes if required. Would you kindly take a look and give your approval?

evgenii-kuznetsov avatar May 10 '24 10:05 evgenii-kuznetsov

please file a PR to start with and we'll review the PR accordingly.

wing328 avatar May 10 '24 10:05 wing328

I've created the PR with change.

evgenii-kuznetsov avatar May 12 '24 10:05 evgenii-kuznetsov