OpenAPI 3.0中java.util.Currency生成独立Schema的方案问询
我来帮你解决这两个问题,结合swagger-core和openapi-generator的特性,有以下可行方案:
问题1:如何通过注解/配置使java.util.Currency生成为独立Schema
由于java.util.Currency是JDK自带类,没法直接在类上添加Swagger注解,我们可以通过两种方式让它在OpenAPI 3.0规范中成为独立的Schema:
字段注解+插件配置强制生成Schema
在你的Wrapper类的Currency字段上,添加@Schema(ref = "#/components/schemas/Currency"),明确指定该字段引用独立的Currency Schema。然后在swagger-maven-plugin的配置里,把java.util.Currency加入additionalModels列表,强制插件为它生成独立的Schema定义:<plugin> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>2.2.15</version> <executions> <execution> <goals> <goal>resolve</goal> </goals> <configuration> <outputFilePath>${project.build.directory}/openapi.json</outputFilePath> <resourcePackages> <package>com.your.package</package> </resourcePackages> <!-- 强制生成Currency的独立Schema --> <additionalModels> <model>java.util.Currency</model> </additionalModels> </configuration> </execution> </executions> </plugin>这样生成的OpenAPI规范里,
Currency会作为独立条目出现在components/schemas下,Wrapper的字段会直接引用这个Schema。自定义ModelConverter扩展Swagger Core
如果需要更灵活的控制(比如自定义Currency的属性展示),可以编写一个ModelConverter来拦截Currency类型的处理,强制生成独立Schema:public class CurrencyModelConverter implements ModelConverter { @Override public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) { Class<?> clazz = (type.getType() instanceof Class) ? (Class<?>) type.getType() : null; if (Currency.class.isAssignableFrom(clazz)) { // 创建Currency的Schema定义 Schema currencySchema = new Schema() .name("Currency") .type("object") .addProperty("currencyCode", new Schema().type("string")) .addProperty("numericCode", new Schema().type("integer")); // 将Schema注册到上下文,确保生成独立定义 context.defineModel("Currency", currencySchema, type); // 返回引用,让字段指向这个独立Schema return new Schema().$ref("#/components/schemas/Currency"); } return chain.next().resolve(type, context, chain); } }然后在swagger-maven-plugin里注册这个转换器:
<configuration> <!-- 其他配置 --> <modelConverters> <modelConverter>com.your.package.CurrencyModelConverter</modelConverter> </modelConverters> </configuration>
问题2:如何让--import-mappings(或等价配置)绑定到属性类型
当Currency是Wrapper的内嵌属性而非独立Schema时,openapi-generator会自动生成WrapperCurrency类。要让生成的代码直接使用java.util.Currency,可以用以下两种方法:
使用
typeMappings映射自动生成的类名typeMappings可以将openapi-generator生成的类名直接映射到指定的Java类,即使这个类型是内嵌属性。在openapi-generator-maven-plugin中配置:<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>6.6.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.build.directory}/openapi.json</inputSpec> <generatorName>java</generatorName> <configOptions> <sourceFolder>src/main/java</sourceFolder> </configOptions> <!-- 将自动生成的WrapperCurrency映射到JDK的Currency --> <typeMappings> <typeMapping>WrapperCurrency=java.util.Currency</typeMapping> </typeMappings> </configuration> </execution> </executions> </plugin>在OpenAPI规范中添加
x-java-type扩展
如果你能修改生成的OpenAPI JSON,可以给Wrapper的currency属性加上x-java-type扩展,直接指定对应的Java类:"components": { "schemas": { "Wrapper": { "type": "object", "properties": { "currency": { "type": "object", "x-java-type": "java.util.Currency" } } } } }这样openapi-generator在生成代码时,会直接用
java.util.Currency作为该属性的类型,无需额外映射配置。
内容的提问来源于stack exchange,提问作者Ivafo

