OpenAPI CodeGen Maven编译报错:找不到oas_any_type_not_mapped类
编译错误:找不到
oas_any_type_not_mapped类(OpenAPI Generator 4.3.1) 使用openapi-generator-cli-4.3.1.jar生成Java客户端代码后,执行Maven命令mvn package编译时出现大量错误,核心问题为无法找到符号oas_any_type_not_mapped类。
提供的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.openapitools</groupId> <artifactId>openapi-java-client</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>openapi-java-client</name> <description>OpenAPI Java</description> <url>https://github.com/openapitools/openapi-generator</url> <scm> <connection>scm:git:git@github.com:openapitools/openapi-generator.git</connection> <developerConnection>scm:git:git@github.com:openapitools/openapi-generator.git</developerConnection> <url>https://github.com/openapitools/openapi-generator</url> </scm> <licenses> <license> <name>Unlicense</name> <url>http://unlicense.org</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <name>OpenAPI-Generator Contributors</name> <email>team@openapitools.org</email> <organization>OpenAPITools.org</organization> <organizationUrl>http://openapitools.org</organizationUrl> </developer> </developers> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <fork>true</fork> <meminitial>128m</meminitial> <maxmem>512m</maxmem> <compilerArgs> <arg>-Xlint:all</arg> <arg>-J-Xss4m</arg><!-- Compiling the generated JSON.java file may require larger stack size. --> </compilerArgs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0-M1</version> <executions> <execution> <id>enforce-maven</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireMavenVersion> <version>2.2.0</version> </requireMavenVersion> </rules> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M4</version> <configuration> <systemProperties> <property> <name>loggerPath</name> <value>conf/log4j.properties</value> </property> </systemProperties> <argLine>-Xms512m -Xmx1500m</argLine> <parallel>methods</parallel> <threadCount>10</threadCount> </configuration> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- attach test jar --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>jar</goal> <goal>test-jar</goal> </goals> </execution> </executions> <configuration> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <id>add_sources</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main\java</source> </sources> </configuration> </execution> <execution> <id>add_test_sources</id> <phase>generate-test-sources</phase> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>src/test/java</source> </sources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <doclint>none</doclint> <tags> <tag> <name>http.response.details</name> <placement>a</placement> <head>Http Response Details:</head> </tag> </tags> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>sign-artifacts</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>${swagger-core-version}</version> </dependency> <!-- @Nullable annotation --> <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>${okhttp-version}</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>logging-interceptor</artifactId> <version>${okhttp-version}</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson-version}</version> </dependency> <dependency> <groupId>io.gsonfire</groupId> <artifactId>gson-fire</artifactId> <version>${gson-fire-version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3-version}</version> </dependency> <dependency> <groupId>org.threeten</groupId> <artifactId>threetenbp</artifactId> <version>${threetenbp-version}</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>${javax-annotation-version}</version> </dependency> <!-- test dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <gson-fire-version>1.8.4</gson-fire-version> <swagger-core-version>1.5.24</swagger-core-version> <okhttp-version>3.14.7</okhttp-version> <gson-version>2.8.6</gson-version> <commons-lang3-version>3.10</commons-lang3-version> <threetenbp-version>1.4.3</threetenbp-version> <javax-annotation-version>1.3.2</javax-annotation-version> <junit-version>4.13</junit-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
错误日志片段
[INFO] Changes detected - recompiling the module! [INFO] Compiling 118 source files to C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\src\main\java\org\openapitools\client\api\ProjectApi.java:[39,36] error: cannot find symbol symbol: class oas_any_type_not_mapped location: package org.openapitools.client.model [ERROR] C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\src\main\java\org\openapitools\client\api\ProjectApi.java:[151,11] error: cannot find symbol symbol: class oas_any_type_not_mapped location: class ProjectApi [ERROR] C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\src\main\java\org\openapitools\client\api\ProjectApi.java:[171,23] error: cannot find symbol symbol: class oas_any_type_not_mapped location: class ProjectApi [ERROR] C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\src\main\java\org\openapitools\client\api\ProjectApi.java:[193,172] error: cannot find symbol symbol: class oas_any_type_not_mapped location: class ProjectApi [ERROR] C:\Users\fd364c\OpenAPI_CodeGen_Fabric\mdultraClient\src\main\java\org\openapitools\client\model\ApiRepositoriesRepoZonePATCH.java:[27,36] error: cannot find symbol symbol: class oas_any_type_not_mapped
解决方案
- 检查OpenAPI规范:规范中如果存在
type: any或未明确映射的对象类型(比如仅声明type: object但没有properties的schema),旧版本的OpenAPI Generator可能无法自动生成对应的oas_any_type_not_mapped类,需确认这类定义是否存在。 - 手动添加缺失类:在
org.openapitools.client.model包下创建oas_any_type_not_mapped.java类,代码如下:
注意类名要和报错中的小写名称保持一致。package org.openapitools.client.model; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; public class oas_any_type_not_mapped { private final Map<String, Object> additionalProperties = new HashMap<>(); @JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return additionalProperties; } @JsonAnySetter public void setAdditionalProperty(String name, Object value) { additionalProperties.put(name, value); } } - 升级OpenAPI Generator版本:4.3.1版本较旧,后续的5.x、6.x版本修复了大量生成逻辑问题,升级后重新生成代码,通常会自动创建该类。
- 调整生成命令参数:重新生成代码时添加
--additional-properties generateAnyType=true参数,强制生成anyType对应的类。
内容的提问来源于stack exchange,提问作者Javier Herrero
相关产品推荐
相关产品推荐

