swagger codegen 需要注释

Annotation needed from swagger codegen

我需要一种方法来注释我的 openapi 3 规范,以便 swagger-codegen 将注释添加到我的 java class,例如 @JsonIgnoreProperties(ignoreUnknown = true)

这可能吗?

TIA!

看来您可以利用小胡子模板。从代码生成 jar 文件中提取所需语言的小胡子文件,并编辑所需的 class 模板文件。然后,生成带有 -t pathToTemplates 标志的客户端代码,如下所示:

java -jar swagger-codegen-cli-2.3.1.jar generate -t C:\SwaggerTemplates -i C:\Swagger.json -l csharp -o C:\Output

是的,有可能。

以下是对我有用的步骤 -

  1. 从 github 下载 swagger-code-gen 源代码或获取 zip。我有 2.3.1 版并从 swagger-code-gen 2.3.1

  2. 下载
  3. 对于 Java,更新 AbstractJavaCodegen.java 文件并在 processOpts( ) 方法:

importMapping.put("JsonIgnoreProperties","com.fasterxml.jackson.annotation.JsonIgnoreProperties");

// 如果导入了 ApiModel,则导入 JsonIgnoreProperties importMapping.put("io.swagger.annotations.ApiModel","com.fasterxml.jackson.annotation.JsonIgnoreProperties");

  1. 保存文件并mvn clean install在目标目录下生成swagger-code-gen-cli-2.3.1

  2. 现在将 "pojo.mustache" 和任何其他所需文件从上述 cli jar(位于此路径 - "swagger-codegen-2.3.1\modules\swagger-codegen-cli\target\swagger-codegen-cli-2.3.1.jar\Java\" 中)提取到一个目录(例如 spring_template )

  3. 在"pojo.mustache"文件的@ApiModel下面添加一行“@JsonIgnoreProperties(ignoreUnknown = true)”

  4. 现在使用构建的 cli jar 和路径中的 spring_template 执行命令: java -jar swagger-codegen-cli-2.3.1.jar 生成 --additional-properties apiPackage=com.xxx.xxx.api,modelPackage=com.xxx.xxx.model,delegatePattern=true,useTags=true, configPackage=com.xxx.xxx.configuration,basePackage=com.xxx.xxx -o app -l spring -i your_swagger_yaml_file.yaml -t spring_template

  5. 这应该使用@JsonIgnoreProperties(ignoreUnknown = true) 并在 class.[= 中导入 com.fasterxml.jackson.annotation.JsonIgnoreProperties 来构建和生成 pojo/ 模型 classes 13=]

maven 有 swagger 插件

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
   <executions>
    <execution>
      <id>java-codegen</id>
      <phase>generate-sources</phase>
         <goals>
            <goal>generate</goal>
         </goals>

         <configuration>
                          
           <language>jaxrs-spec</language>
           <templateDirectory>${project.build.directory}/swagger-templates</templateDirectory>
           <inputSpec>${basedir}/src/main/resources/v1/swagger.yaml</inputSpec>
           <output>${project.build.directory}/swagger-codegen</output>
           <apiPackage>com.example.api.v1</apiPackage>
           <modelPackage>com.example.api.v1.dto</modelPackage>
           <modelNameSuffix>DTO</modelNameSuffix>
           <configOptions>
             <additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>
             <dateLibrary>java8</dateLibrary>
           </configOptions>

         </configuration>
       </execution>
     </executions>
   </plugin>

如果我们使用swagger-codegen-maven-plugin生成代码那么

<additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>

将使用 @JsonIgnoreProperties(ignoreUnknown = true)

生成 DTO