javahandlebars.jsmustacheopenapi-generator

OpenAPI Custom Generator - How do I prevent "AllOf" Class Generation in OpenApi


I'm building a custom generator to generate TypeScript/Angular models for an angular application. For a starting point, I copied the code from https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java

I'm trying to figure out how to prevent the generator from generating "AllOf" and from extending models from "AllOf" models.

The generator already generates a model with everything it needs without needing to extend from *AllOf.

I've been able to modify the .java file to prevent it from importing classes that with with "AllOf", but I can't find any documentation or examples to restrict extending from classes that end with "AllOf"

Am I missing something? It seems like there should be a way to tell the generator to just not import or create "AllOf" classes.

// what I get:
import { ValidatePassword } from './validate-password.model';

export interface ChangePassword extends ChangePasswordAllOf, ValidatePassword { 
    ...
}

// what I want
import { ValidatePasswordModel } from './validate-password.model';

export interface ChangePassword extends ValidatePassword { 
    ...
}

Here's my modelGeneric.mustache template:

export interface {{classname}}{{#allOf}}{{#-first}} extends {{/-first}}{{{.}}}{{^-last}}, {{/-last}}{{/allOf}} { {{>modelGenericAdditionalProperties}}
{{#vars}}
    {{#description}}
    /**
     * {{{.}}}
     */
    {{/description}}
    {{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}}
}{{>modelGenericEnums}}

Here's the relevant schema sample:

...
"ChangePassword": {
  "allOf": [
    {
      "$ref": "#/components/schemas/ValidatePassword"
    },
    {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "OldPassword"
      ],
      "properties": {
        "OldPassword": {
          "title": "Current password",
          "type": "string",
          "minLength": 1
        }
      }
    }
  ]
},
...

Solution

  • I think this has already been addressed in OpenAPI Generator v7.4.0. Please give it another try when you've time.

    Another useful option is enabling the rule REF_AS_PARENT_IN_ALLOF in OpenAPI Normalizer