我正在使用JHipster和OpenAPI生成Spring Boot项目。使用JHipster生成项目后,我将OpenAPI文件复制到src/main/resources/swagger/api.yml
中,并使用mvnw generate-sources
运行OpenAPI子生成器。它失败,并带有大量异常(请参见下文)。
版本信息:
注意:还有很多与此相关的其他SO问题(How to define an array of another schema in OpenAPI 3?和Return an array of object in Swaggerhub),并且我的文件的结构也相同。这对我来说很有意义……但这是行不通的。
所以,这就是我所拥有的(文件底部的模式定义):
# API-first development with OpenAPI
# This file will be used at compile time to generate Spring-MVC endpoint stubs using openapi-generator
openapi: '3.0.1'
info:
title: 'testEchoMicroservice'
version: 0.0.1
servers:
- url: http://localhost:9100
description: Development server
- url: https://localhost:9100
description: Development server with TLS Profile
paths:
/random:
get:
deprecated: true
summary: Returns a random message
description: Returns a random message
responses:
'200':
description: Random response
content:
application/json:
schema:
$ref: '#/components/schemas/Echo'
/echo:
post:
summary: Echoes a specified message
description: Echoes a specified message
requestBody:
description: Echo request
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Echo'
responses:
'200':
description: Echo response
content:
application/json:
schema:
$ref: '#/components/schemas/Echo'
/history:
get:
summary: Get echo history
description: Get echo history
responses:
'200':
description:
content:
application/json:
schema:
$ref: '#/components/schemas/EchoList'
components:
securitySchemes:
jwt:
type: http
description: JWT Authentication
scheme: bearer
bearerFormat: JWT
schemas:
Echo:
type: object
properties:
message:
type: string
EchoList:
type: array
items:
$ref: '#/components/schemas/Echo'
security:
- jwt: []
因此,EchoList
只是Echo
个对象的数组。
当我尝试运行jhipster openapi子生成器时,它引发以下错误(注意:由于它超出了SO限制,因此未包含相当多的堆栈跟踪。经过快速检查,它重复了下面看到的异常):
> mvnw generate-sources
...
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
Failed while enforcing releasability. See above detailed error message.
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.4:prepare-agent (pre-unit-tests) @ echo-service ---
[INFO] argLine set to -javaagent:C:\\Users\\JO24447\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.4\\org.jacoco.agent-0.8.4-runtime.jar=destfile=C:\\Users\\JO24447\\workspace\\OpenAPI\\entity-test\\target\\jacoco\\test\\test.exec -Djava.security.egd=file:/dev/./urandom -Xmx256m
[INFO]
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (default) @ echo-service ---
[INFO]
[INFO] --- openapi-generator-maven-plugin:4.1.3:generate (default) @ echo-service ---
[WARNING] Exception while reading:
com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: mapping values are not allowed here
in 'reader', line 73, column 18:
items: $ref: '#/components/schemas/Echo'
^
at [Source: (StringReader); line: 73, column: 18]
at com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException.from (MarkedYAMLException.java:27)
at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken (YAMLParser.java:359)
at com.fasterxml.jackson.core.JsonParser.nextFieldName (JsonParser.java:825)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:247)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize (JsonNodeDeserializer.java:68)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize (JsonNodeDeserializer.java:15)
at com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose (ObjectMapper.java:4056)
at com.fasterxml.jackson.databind.ObjectMapper.readTree (ObjectMapper.java:2551)
at io.swagger.v3.parser.OpenAPIV3Parser.readWithInfo (OpenAPIV3Parser.java:154)
at io.swagger.v3.parser.OpenAPIV3Parser.readLocation (OpenAPIV3Parser.java:58)
at io.swagger.parser.OpenAPIParser.readLocation (OpenAPIParser.java:16)
at org.openapitools.codegen.config.CodegenConfigurator.toContext (CodegenConfigurator.java:410)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput (CodegenConfigurator.java:458)
at org.openapitools.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:705)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:61)
Caused by: org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
in 'reader', line 73, column 18:
items: $ref: '#/components/schemas/Echo'
^
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchValue (ScannerImpl.java:870)
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens (ScannerImpl.java:358)
at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken (ScannerImpl.java:227)
at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce (ParserImpl.java:558)
at org.yaml.snakeyaml.parser.ParserImpl.peekEvent (ParserImpl.java:158)
at org.yaml.snakeyaml.parser.ParserImpl.getEvent (ParserImpl.java:168)
at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken (YAMLParser.java:355)
at com.fasterxml.jackson.core.JsonParser.nextFieldName (JsonParser.java:825)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:247)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject (JsonNodeDeserializer.java:255)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize (JsonNodeDeserializer.java:68)
at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize (JsonNodeDeserializer.java:15)
at com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose (ObjectMapper.java:4056)
at com.fasterxml.jackson.databind.ObjectMapper.readTree (ObjectMapper.java:2551)
at io.swagger.v3.parser.OpenAPIV3Parser.readWithInfo (OpenAPIV3Parser.java:154)
at io.swagger.v3.parser.OpenAPIV3Parser.readLocation (OpenAPIV3Parser.java:58)
at io.swagger.parser.OpenAPIParser.readLocation (OpenAPIParser.java:16)
at org.openapitools.codegen.config.CodegenConfigurator.toContext (CodegenConfigurator.java:410)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput (CodegenConfigurator.java:458)
at org.openapitools.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:705)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:61)
[WARNING] Error snake-parsing yaml content
io.swagger.parser.util.DeserializationUtils$SnakeException: Exception safe-checking yaml content (maxDepth 2000)
at io.swagger.parser.util.DeserializationUtils$CustomSnakeYamlConstructor.getSingleData (DeserializationUtils.java:300)
at org.yaml.snakeyaml.Yaml.loadFromReader (Yaml.java:524)
at org.yaml.snakeyaml.Yaml.load (Yaml.java:437)
at io.swagger.parser.util.DeserializationUtils.readYamlTree (DeserializationUtils.java:137)
at io.swagger.parser.Swagger20Parser.deserializeYaml (Swagger20Parser.java:83)
at io.swagger.parser.Swagger20Parser.readWithInfo (Swagger20Parser.java:64)
at io.swagger.parser.SwaggerParser.readWithInfo (SwaggerParser.java:32)
at io.swagger.v3.parser.converter.SwaggerConverter.readLocation (SwaggerConverter.java:96)
at io.swagger.parser.OpenAPIParser.readLocation (OpenAPIParser.java:16)
at org.openapitools.codegen.config.CodegenConfigurator.toContext (CodegenConfigurator.java:410)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput (CodegenConfigurator.java:458)
at org.openapitools.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:705)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:61)
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.520 s
[INFO] Finished at: 2019-11-14T11:15:25-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.openapitools:openapi-generator-maven-plugin:4.1.3:generate (default) on project echo-service: Code generation failed. See above for the full exception. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
答案 0 :(得分:1)
奇怪的是我错过了。显然,我的文件结构必须如下:
components:
...
schemas:
Echo:
type: object
properties:
message:
type: string
EchoList:
type: array
items: $ref: '#/components/schemas/Echo'
因为,将引用放在自己的行上(并缩进)后,现在一切正常。
components:
...
schemas:
Echo:
type: object
properties:
message:
type: string
EchoList:
type: array
items:
$ref: '#/components/schemas/Echo'
多么狡猾,微妙(且易于制造)的陷阱。