gpt4 book ai didi

java - SwaggerGen 枚举生成空数据类型

转载 作者:行者123 更新时间:2023-12-01 16:35:22 28 4
gpt4 key购买 nike

这是我的 build.gradle

buildscript {
ext {
springBootVersion = '2.1.8.RELEASE'
}
repositories {
maven {
url <<local repo not posted here>>
}
maven {
url <<second local repo not posted here>>
}
}
dependencies {
classpath("io.swagger:swagger-codegen:2.4.7")
classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE"
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
}
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
maven {
url <<local repo not posted here>>
}
}


import io.swagger.codegen.DefaultGenerator
import io.swagger.codegen.config.CodegenConfigurator

def swaggerInput = "${rootDir}/BundledExports/API/POMOrchestrator.v1-Bundled.json"
// def swaggerInput = "${rootDir}/API/POMOrchestrator.v1.json"
def templatedir = "${rootDir}/templates".toString()
def swaggerOutputDir = file('application/')


task generateApi {
inputs.file(swaggerInput)
outputs.dir(swaggerOutputDir)
doLast {
def config = new CodegenConfigurator()
config.setInputSpec(swaggerInput)
config.setOutputDir(swaggerOutputDir.path)
config.setIgnoreFileOverride(swaggerOutputDir.path)
config.setLang('spring')
config.setAdditionalProperties([
'modelPackage' : 'com.party.onboarding.models',
'modelNamePrefix': 'Party',
'dateLibrary' : 'java8',
'templateDir' : templatedir,
'interfaceOnly' : 'true' // Generating the Controller API interface and the models only

])
config.setImportMappings([
'hello': 'com.onbording.model.Hello'
])
new DefaultGenerator().opts(config.toClientOptInput()).generate()
}
}

clean.doFirst {
delete(swaggerOutputDir)
}

configurations {
swagger
}

sourceSets {
swagger {
compileClasspath = configurations.swaggerCompile
java {
srcDir file("${project.buildDir.path}/swagger/src/main/java")
}
}
main {
compileClasspath += swagger.output
runtimeClasspath += swagger.output
}
test {
compileClasspath += swagger.output
runtimeClasspath += swagger.output
}
}

compileSwaggerJava.dependsOn generateApi
classes.dependsOn swaggerClasses
compileJava.dependsOn compileSwaggerJava

ext {
springBootVersion = '2.1.8.RELEASE'

swaggerVersion = '2.7.0'
springCloudServicesVersion = '2.1.2.RELEASE'
springCloudVersion = 'Greenwich.RC1'
cucumberVersion = '2.3.1'

jackson_version = '2.4.2'
jersey_version = '1.18'
jodatime_version = '2.3'
junit_version = '4.8.1'
}

dependencies {
swaggerCompile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
swaggerCompile 'io.swagger:swagger-annotations:1.5.16'
swaggerCompile 'com.squareup.okhttp:okhttp:2.7.5'
swaggerCompile 'com.squareup.okhttp:logging-interceptor:2.7.5'
swaggerCompile 'com.google.code.gson:gson:2.8.1'

compile sourceSets.swagger.output

compile "com.sun.jersey:jersey-client:$jersey_version"
compile "com.sun.jersey.contribs:jersey-multipart:$jersey_version"
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
compile "com.fasterxml.jackson.datatype:jackson-datatype-joda:2.1.5"
compile "joda-time:joda-time:$jodatime_version"
compile 'io.swagger:swagger-codegen:2.2.3'

testCompile "junit:junit:$junit_version"

runtime 'com.squareup.okhttp:okhttp:2.7.5'
runtime 'com.squareup.okhttp:logging-interceptor:2.7.5'
runtime 'com.google.code.gson:gson:2.8.1'
}

接受有关如何改进的建议

这是我的枚举 mustache 文件

  /**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
@Getter
@AllArgsConstructor
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}
{{#enumVars}}
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}
{{^gson}}
{{#allowableValues}}
{{#enumVars}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}

private {{{dataType}}} value;

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (b.value.equals(value)) {
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
}
}

这是枚举外 mustache 类

{{#jackson}}
import com.fasterxml.jackson.annotation.JsonCreator;
{{/jackson}}
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
@Getter
@AllArgsConstructor
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}
{{#enumVars}}
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}
{{^gson}}
{{#allowableValues}}
{{#enumVars}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}

private {{{dataType}}} value;

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (b.value.equals(value)) {
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
}
}

这是一个非常大的 json 文件的示例

    "knowledgeLevelCode": {
"type": "string",
"enum": [
"EXTENSIVE",
"GOOD",
"LIMITED",
"NONE",
"DECLINED"
],
"description": "Knowledge Level Code."

这是代码

  @Getter
@AllArgsConstructor
public enum KnowledgeLevelCodeEnum {
EXTENSIVE("EXTENSIVE"),

GOOD("GOOD"),

LIMITED("LIMITED"),

NONE("NONE"),

DECLINED("DECLINED");

private value;

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static KnowledgeLevelCodeEnum fromValue( value) {
for (KnowledgeLevelCodeEnum b : KnowledgeLevelCodeEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}

不确定是什么导致它丢失数据类型。

有趣的是,如果我使用 OpenApi,则会放入数据类型,但是嵌套包含存在一个问题,导致它使几种实际类型只是普通对象。

最佳答案

好吧,经过大量搜索后我找到了答案。基本上这是数据类型的区分大小写问题。他们在某个地方将数据类型更改为 dataType 但我用来生成的版本需要它是小写 t。所以改成这样就可以了。

{{#jackson}}
import com.fasterxml.jackson.annotation.JsonCreator;
{{/jackson}}
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
@Getter
@AllArgsConstructor
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}
{{#enumVars}}
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}
{{^gson}}
{{#allowableValues}}
{{#enumVars}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/gson}}

private {{{datatype}}} value;

@JsonCreator
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{datatype}}} value) {
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
if (b.value.equals(value)) {
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
}
}

关于java - SwaggerGen 枚举生成空数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960838/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com