Utilize openapi-generator-cli in C# to tailor the names of properties and types created for oneOf requestBodies and polymorphic properties

I am working with an API that involves complex DTOs with polymorphic properties and endpoints that accept polymorphic request bodies (oneOf).

Here is an example of a C# complex type:

Example c# complex type

namespace Sample {

    public class ComplexType
    {
        public PolymorphicTypeBase PolymorphicProperty { get; set; }
    }

    public abstract class PolymorphicTypeBase {
        public string SomeSharedProperty { get; set; }
    }

    public class PolymorphicSubtypeA : PolymorphicTypeBase
    {
        public string PropertyA { get; set; }
    }

    public class PolymorphicSubtypeB : PolymorphicTypeBase
    {
        public string PropertyB { get; set; }
    }

}

The openAPI specification for these types appears as follows:

...
{
  "ComplexType" : {
    "type" : "object", 
    "properties" : {
      "polymorphicProperty" : {
          "oneOf": [
            { "$ref": "#/components/schemas/PolymorphicTypeA" },
            { "$ref": "#/components/schemas/PolymorphicTypeB" }
          ],
          "nullable": true
      }
    }
  },
  "PolymorphicTypeBase": {
    "required": ["discriminator"],
    "type": "object",
    "properties": {
      "discriminator": {
        "type": "string",
        "nullable": true
      },
      "someSharedProperty": {
        "type": "string",
        "nullable": true
      },
    },
    "additionalProperties": false,
    "discriminator": {
      "propertyName": "discriminator",
      "mapping": {
        "PolymorphicTypeA": "#/components/schemas/PolymorphicTypeA",
        "PolymorphicTypeB": "#/components/schemas/PolymorphicTypeB",
      }
    }
  },
  "PolymorphicTypeA" : {
    "type": "object",
    "allOf": [
      {
        "$ref": "#/components/schemas/PolymorphicTypeBase"
      }
    ],
    "properties": {
      "propertyA": {
        "type": "string",
        "nullable": true
      }
    },
    "additionalProperties": false
  },
  "PolymorphicTypeB" : {
    "type": "object",
    "allOf": [
      {
        "$ref": "#/components/schemas/PolymorphicTypeBase"
      }
    ],
    "properties": {
      "propertyB": {
        "type": "string",
        "nullable": true
      }
    },
    "additionalProperties": false
  }
}
...

THE ISSUE

I want the openapi-generator-cli to generate models in the client library that resemble the structure of the ComplexType, where the PolymorphicProperty is of type PolymorphicTypeBase. Instead, what I currently get is an extra type called ComplexTypePolymorphicProperty, which contains annotations for known subtypes but does not reference PolymorphicTypeBase anywhere. This occurs because the information about the base type (PolymorphicTypeBase) seems to be lost in the openAPI spec.

Question

Is there a way to reference the base type in the openAPI spec?

Can we customize the type and property names produced by the c#, typescript-angular, and java generators?

Answer №1

My issue was resolved by incorporating custom schema and request body filters to update the existing components' definitions with references ($ref) instead of using oneOf

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conditional Return Types in a Typescript Function

There is a function that can return two different types, as shown below: function doSomething(obj: {a: string, b?: string}): string | number { if (obj.b) { return 'something' } return 1 } When the function is called with an object cont ...

Issue with assigning object properties in Internet Explorer 11 with ECMAScript

I recently made some changes to my Angular7 app, including an upgrade to Angular7 and upgrading package dependencies. However, I encountered an issue when testing my app on PROD Internet Explorer 11. While there are no problems when testing on localhost, o ...

Accessing all the key-value pairs in a String array object

Looking to retrieve specific elements like host/username from the following string format. { "username":"admin", "password":"admin1234", "engine":"mysql", "host":"toolsdata.us-e ...

An error was encountered in compiler.js at line 1021, stating that an unexpected value 'UserService' was imported by the module 'UserModule'. It is recommended to add a @NgModule annotation to resolve this issue

As a PHP programmer new to Angular, I am facing an issue while trying to retrieve user properties from a Laravel API. When attempting this, I encountered the following error: compiler.js:1021 Uncaught Error: Unexpected value 'UserService' importe ...

error message: when running `prisma generate`, the installed package `@tsed/prisma` cannot be located

Attempting to integrate Ts.Ed v7.35 with prisma v5.2 by following this official tutorial. Upon running npm install, encountered an error during npx prisma generate: Environment variables loaded from .env Prisma schema loaded from prisma/schema.prisma Erro ...

Allowing access for a unique Windows account on a specific ASP.NET page

In my company, we have a report page named report.aspx. I want to restrict access to this page to only specific Windows accounts. For instance, we have 4 different Windows Account Names in our Active Directory: AA, BB, CC, and DD. We only want these 4 us ...

Vue.js with TypeScript: The property 'xxx' is not found on the type 'never'

I have a computed method that I am trying to execute: get dronesFiltered(){ const filtered = this.drones.filter((drone) => { return drone.id.toString().indexOf(this.filterId) > -1 && drone.name.toLowerCase().toString().in ...

Issue with Ionic 2's infinite scroll not triggering method on second scroll attempt

I utilized the ion-tab element to showcase a page (inboxitem) which encompasses ion-list and makes use of ion-infinite-scroll. The following code snippet resides in inboxitem.html <ion-content class="inbox can-swipe-list"> <ion-list> ...

Using Asynctask with HttpClient in Android

After spending a day or two stuck on this problem, I decided to seek help from the community. My current objective is to create an HttpPost request that connects to and executes a script hosted on my Wamp server. I have developed a class that extends Asyn ...

The object literal's property 'children' is assumed to have a type of 'any[]' by default

Is there a way to assign the property myOtherKey with any value? I encountered a Typescript error that says.. A problem occurred while initializing an object. The property 'children' in the object literal implicitly has an array type of 'a ...

"Creating a multi-dimensional array in Java by setting its dimensions to match another array's size, without copying over the elements

I am looking to create a new array that is the same size as an existing multidimensional array, without copying the elements. I have an example: int table[1][2]; table[0][0] = '1'; table[0][1] = '2'; table[0][2] = '3'; table ...

What are the steps to resolve the UglifyJs error stating 'Unexpected token operator'?

When running the following command in my Angular app: > ng build --prod --aot --env=staging I encounter this error message: ERROR in vendor.0625f773941bc83e6748.bundle.js from UglifyJs Unexpected token operator «*», expected punc «(» [vendor.0625 ...

Array of strings displaying inaccurate values

I am having issues with extracting the values of strings and integers in my code. I tried storing them in arrays but ended up not getting the correct string values. Can someone please help me identify the error in my code? public static void main(String[] ...

Looking to organize a map by adjusting both the keys and values?

Hey there! I'm working on sorting through a Map and I'm encountering a little hiccup. I tried to come up with my own way of doing it, but unfortunately, my program isn't behaving as expected. Here's the code snippet I've been worki ...

Error in browsers when declaring an interface in Angular

Currently, I'm enrolled in an online Angular course to brush up on my skills. However, during one of the initial classes, I encountered an error while declaring an interface. interface face{ name: string; address: string; } The error message ...

JSON objects within a loop may not consistently be present

I'm grappling with a complex JSON object that presents a challenge. The issue arises when certain elements within the JSON structure are missing, leading to errors in parsing and looping through the entire list. While some elements return complete da ...

Is there a way to receive the ultimate discounted price instead of just the discount amount deducted?

I have been working on a function that takes in a percentage as an argument and reduces the price by that percentage. public void reducePriceBy(double percent){ price = price - (price * percent/100); } How can I modify my function to return the final ...

Jackson: Is there a way to modify the indentation of the colon in the JSON output?

I have a JSON output with an unwanted whitespace on the left side of the colons that I need to remove. Displayed below is the output, followed by the code that generates it. I must preserve all other formatting in the output and can't remove pretty ...

Angular component: The selected property is not appearing set to false on the first click when using @

I am currently working on developing a set of accordion components. My goal is to have only one accordion open at a time out of the three available. Essentially, if one accordion is open and I click on another one, the open accordion should collapse. At t ...

Resolving the Challenge of Clicking on a Web Element

My webpage can have multiple datepickers on a single page. Initially, I use a loop to determine the number of datepickers present on the page. List<WebElement> calanders = driver.findElements(By.cssSelector(".c-input-group__addon")); ...