简体   繁体   English

OpenAPI 自定义生成器 - 如何防止 OpenApi 中的“AllOf”类生成

[英]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.我正在构建一个自定义生成器来为角度应用程序生成 TypeScript/Angular 模型。 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首先,我从https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen复制了代码。爪哇

I'm trying to figure out how to prevent the generator from generating "AllOf" and from extending models from "AllOf" models.我试图弄清楚如何防止生成器生成“AllOf”和从“AllOf”模型扩展模型。

The generator already generates a model with everything it needs without needing to extend from *AllOf.生成器已经生成了一个包含它需要的一切的模型,而无需从 *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"我已经能够修改 .java 文件以防止它导入带有“AllOf”的类,但是我找不到任何文档或示例来限制从以“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.似乎应该有一种方法可以告诉生成器不要导入或创建“AllOf”类。

// 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:这是我的modelGeneric.mustache模板:

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
        }
      }
    }
  ]
},
...

According to this: https://github.com/OpenAPITools/openapi-generator/issues/3100根据这个: https : //github.com/OpenAPITools/openapi-generator/issues/3100

Could you try:你能不能试试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM