OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP] Issue with readOnly fields and inheritance for csharp-netcore generator

Open
#5,298 0 comments 0 reactions 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? Yes
  • Have you validated the input using an OpenAPI validator (example)? Yes
  • What's the version of OpenAPI Generator used? latest
  • Have you search for related issues/PRs? Yes
  • What's the actual output vs expected output? see below
  • [Optional] Bounty to sponsor the fix (example)
Description

I found an issue when a schema with a readOnly field is referred to in another Schema
For example:

    Cat:
      type: object
      allOf:
        - $ref: "#/components/schemas/Pet"

    Pet:
      type: object
      properties:
        id:
          type: integer
          format: int64
          readOnly: true

The client generator for csharp-netcore creates a Cat class that inherits Pet (that works fine) and defines a constructor including all the fields present in Pet

        public Cat() : base(id)
        {
        }

There is no constructor in the Pet class with such signature as id is a readOnly field.

openapi-generator version

csharp-netcore

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /cats/{petId}:
    get:
      summary: Info for a specific cat
      operationId: showCatById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Cat"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
          format: int64
          readOnly: true
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Cat:
      type: object
      allOf:
        - $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
Command line used for generation

openapi-generator generate -i petstore.yaml -g csharp-netcore -o petstore

Steps to reproduce
  • Generate the client from the specification yaml
  • Try to build to solution
Suggest a fix

The generator should check for readOnly fields and skip them when referencing the base constructor to avoid this issue

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 by running the provided openapi-generator command with the Pet and Cat schemas, then inspect the generated Cat and Pet constructors. Confirm the generated client builds successfully and that readOnly fields are not passed to a base constructor that does not accept them.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.