OpenAPITools / OpenAPITools/openapi-generator

[BUG] [csharp-netcore] ToJson is not marked virtual even though it's overridden

Open
#5,309 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

For an OpenAPI declaration with two component schemas, one of which includes properties form the other using allOf, when using the csharp-netcore generator, OpenAPI Generator will generate a non-virtual ToJson method in a base class that is overridden in a derived class. This seems to happen when the base schema has additionalProperties defined.

For the OpenAPI declaration file below, I would expect OpenAPI Generator to generate the following method declaration in BaseClass:

public virtual string ToJson()
{
   return JsonConvert.SerializeObject(this, Formatting.Indented);
}

However, it actually generates this one:

public string ToJson()
{
   return JsonConvert.SerializeObject(this, Formatting.Indented);
}

In the DerivedClass definition (public partial class DerivedClass : BaseClass), this leads to the ToJson method definition being invalid:

public override string ToJson()
{
    return JsonConvert.SerializeObject(this, Formatting.Indented);
}

The compiler error generated is: Error CS0506 'DerivedClass.ToJson()': cannot override inherited member 'BaseClass.ToJson()' because it is not marked virtual, abstract, or override Org.OpenAPITools {PATH}\src\Org.OpenAPITools\Model\DerivedClass.cs line 64

openapi-generator version

Version 4.2.3

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: ToJson Virtual Bug
  version: '1.0'
servers:
  - url: localhost
paths:
  /test-path:
    get:
      responses:
        '200':
          description: Success
components:
  schemas:
    BaseClass:
      required:
        - $type
      type: object
      properties:
        $type:
          type: string
      additionalProperties:
        type: object
        additionalProperties: false
      discriminator:
        propertyName: $type
    DerivedClass:
      type: object
      allOf:
        - $ref: '#/components/schemas/BaseClass'
Command line used for generation

npx -p @openapitools/openapi-generator-cli@cli-4.2.3 openapi-generator generate -i ./tojsonvirtualbug.yaml -g csharp-netcore -o ./tojsonvirtualbug

Steps to reproduce
Related issues/PRs
Suggest a fix

OpenAPI Generator could always output the virtual keyword if the override keyword is not generated. I believe that change would happen here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache at the referenced line, then generate the supplied OpenAPI declaration with the csharp-netcore command. Confirm that BaseClass.ToJson is virtual when DerivedClass overrides it, and verify the generated C# output no longer produces compiler error CS0506.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.