标签: openapi-generator

OpenAPI 生成器可使用 Spring 进行分页

我希望 OpenAPI 生成器(https://github.com/OpenAPITools/openapi-generator)能够根据 Spring Boot Data 中的实现在 API 中生成 Pageable 参数。我一直在尝试寻找一种合适的开箱即用的解决方案,但找不到。

理想情况下,此 Pageable 参数应仅按以下方式添加到 GET 方法:

default ResponseEntity<User> getUser(@ApiParam(value = "value",required=true) @PathVariable("id") Long id, **Pageable pageable**)
Run Code Online (Sandbox Code Playgroud)

因此,在我的控制器中实现此接口后,我需要重写它并具有上述的 Pageable 参数。我不想有单独的大小或页面参数,这里只有这个可分页。

感谢您的任何提示和帮助!

java spring code-generation openapi openapi-generator

13
推荐指数
1
解决办法
2万
查看次数

OpenApi Generator 创建类型为 String 而不是 Date 的模型

我使用OpenApi Generator为我的 Angular 项目创建服务和模型。

这是我的 Json 的示例:

"uploadDate" : {
              "type" : "string",
              "description" : "Data caricamento",
              "format" : "date-time"
            },
Run Code Online (Sandbox Code Playgroud)

当我生成文件时,uploadDate 被转换为字符串而不是日期。

看:

     /**
     * Data caricamento
     */
    uploadDate?: string;
Run Code Online (Sandbox Code Playgroud)

有没有办法添加配置并创建以日期为类型且格式为日期时间的模型?

这是我用来启动生成的脚本:

"generate": "openapi-generator-cli generate -g typescript-angular -i openapi.json -o ./generated-sources/client"
Run Code Online (Sandbox Code Playgroud)

typescript openapi angular openapi-generator

13
推荐指数
1
解决办法
5960
查看次数

如何修复代码生成中看似正确的“inputSpec”中的 OpenAPI 错误?

我正在尝试运行 OpenAPI 生成器,但我不断收到一条没有意义的错误消息。

Failed to execute goal org.openapitools:openapi-generator-maven-plugin:5.0.0-SNAPSHOT:generate (default-cli) on project miguelmunoz.challenge: The parameters 'inputSpec' for goal org.openapitools:openapi-generator-maven-plugin:5.0.0-SNAPSHOT:generate are missing or invalid

问题是我的inputSpec值指向有效的 .yaml 文件。该文件位于src/main/resources/yaml/pizzeria.yaml,我使用复制和粘贴来确保该路径中没有拼写错误。您可以在https://github.com/SwingGuy1024/OpenAPI_inputSpec_Bug下载最小可重现测试用例。

这是我的插件规范:

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <!-- RELEASE_VERSION -->
            <version>5.0.0-SNAPSHOT</version>
            <!-- /RELEASE_VERSION -->
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <!-- General Configuration properties taken from -->
                        <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md -->
                        <!--Changed to https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin-->
                        <!-- Modifications from /sf/ask/4501283461/#64363872 -->
                        <ignoreFileOverride>${project.basedir}/my-springboot.ignores</ignoreFileOverride>  <!--Added-->
                        <inputSpec>${project.basedir}/src/main/resources/yaml/pizzeria.yaml</inputSpec>
                        <!--the language tag was replaced by the generatorName tag:-->
                        <generatorName>spring</generatorName>
                        <!--<templateDirectory>${project.basedir}/src/gen/templates/</templateDirectory>--> …
Run Code Online (Sandbox Code Playgroud)

java jpa openapi openapi-generator

13
推荐指数
1
解决办法
2万
查看次数

用于OpenAPI 3.0的Swagger Codegen(带有maven插件)

我想使用Swagger Codegen for OpenAPI 3.0 YAML文件.我看到Swagger Codegen 3.0.0-rc0可用.但是当我尝试使用它时,我遇到了问题.以下是详细信息:

我的pom.xmlswagger-codegen插件文件:

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.0-rc0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${basedir}/src/main/resources/mySpec.yaml</inputSpec>
        <output>target/generated-sources</output>
        <language>spring</language>
        <generateApis>false</generateApis>
        <modelPackage>com.kj.model</modelPackage>
        <apiPackage>com.kj</apiPackage>
        <configOptions>
          <sourceFolder>swagger</sourceFolder>
          <library>spring-mvc</library>
          <interfaceOnly>true</interfaceOnly>
          <useBeanValidation>true</useBeanValidation>
          <dateLibrary>java8</dateLibrary>
          <java8>true</java8>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

使用上面的插件,当我运行maven构建时,我得到了这个ServiceConfigurationError,这里是堆栈跟踪:

Exception in thread "main" java.util.ServiceConfigurationError: io.swagger.codegen.CodegenConfig: Provider io.swagger.codegen.languages.java.JavaClientCodegen not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:19)
    at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:392)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:512)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我 pom文件的maven插件部分添加了swagger-codegen-generators依赖项:

<dependencies> …
Run Code Online (Sandbox Code Playgroud)

maven swagger openapi swagger-codegen openapi-generator

12
推荐指数
2
解决办法
2万
查看次数

springdoc-openapi-maven-plugin配置/apiDocsUrl的作用是什么?

我正在运行 springdoc-openapi-maven-plugin,具有以下(标准)配置:

        <plugin>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-maven-plugin</artifactId>
            <version>0.2</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

该插件似乎运行,但没有输出。

我在 Maven 输出中收到 404 错误:

[INFO] --- springdoc-openapi-maven-plugin:0.2:generate (integration-test) @ paatinc-util-websrv ---
10:40:33.930 [http-nio-8080-exec-1] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
10:40:33.931 [http-nio-8080-exec-1] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
10:40:33.956 [http-nio-8080-exec-1] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 25 ms
10:40:33.969 [http-nio-8080-exec-1] INFO  io.paat.util.filter.LoggingFilter - GET http://localhost:8080/v3/api-docs from 127.0.0.1
[ERROR] An error has occured: Response code 404
Run Code Online (Sandbox Code Playgroud)

我可以从我的日志中看到 404 正在调用:http://localhost:8080/v3/api-docs

我还在 springdoc-openapi-maven-plugin 文档中看到以下配置:

 <configuration> …
Run Code Online (Sandbox Code Playgroud)

java spring maven openapi openapi-generator

12
推荐指数
2
解决办法
5214
查看次数

HttpMessageNotWritableException:没有带有 OpenApi Spring 生成器的 [...] 预设 Content-Type 'null'] 转换器

我正在(gradle)spring boot 项目中使用 openAPI 实现 petstore API。我使用 openapi 生成器插件生成一个服务器并实现一个简单的请求:

@Service
@RestController
public class PetApiController implements PetApi {

    @Override
    public ResponseEntity<Pet> getPetById(Long petId) {
        Pet p = new Pet();
        p.setId(0L);
        p.setName("fido");
        p.setPhotoUrls(new ArrayList<String>());
        p.setStatus(StatusEnum.AVAILABLE);
        p.setTags(new ArrayList<Tag>());
        Category c = new Category();
        c.setId(3L);
        c.setName("dummy category");
        p.setCategory(c);
        
        ResponseEntity<Pet> r = new ResponseEntity<Pet>(p, HttpStatus.OK);
        
        return r;
    }
}
Run Code Online (Sandbox Code Playgroud)

我生成的 swagger-ui 为请求提供了 xml 和 json 查询,但 xml 似乎不起作用:

$ curl -X GET "http://localhost:8080/pet/1" -H  "accept: application/xml"; echo ""

$ curl -X GET "http://localhost:8080/pet/1" -H  "accept: …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot openapi-generator

12
推荐指数
4
解决办法
5万
查看次数

生成 Spring 代码时如何将 OpenAPI“oneOf”属性与 openapi-generator-maven-plugin 一起使用

我正在开发一个带有 Angular 前端和 RESTful Spring Boot 后端的应用程序

我从 org.openapitools找到了这个非常方便的 maven 插件openapi-generator-maven-plugin。凭借其代码生成功能,它有助于在我们的 API 的前端和后端之间实施“合同优先”的方法。但是我们的 swagger 文件在 requestBody 和 responseBody 定义中使用了“oneOf”属性。我试图从中生成 Spring 代码,但生成的 Java 类缺少导入:

import com.pack.api.dto.OneOfLatteCoffeAmericanoCoffe;
import com.pack.api.dto.UNKNOWN_BASE_TYPE;
Run Code Online (Sandbox Code Playgroud)

是否可以配置插件以使用 Swagger 的 oneOf 属性?我正在使用 Spring Boot 2.3.1、Swagger 3.0 和 Openapi-generator-maven-plugin 4.3

code-generation maven-plugin spring-boot openapi-generator

11
推荐指数
3
解决办法
8431
查看次数

OpenAPI 能否以有用的方式集成 HATEOAS?

是否可以使用 OpenAPI 来描述 HATEOAS REST API?

当我以HAL格式描述 API 时,我需要为其定义三种模式(一种用于请求有效负载,一种用于收集资源,一种用于项目资源)。例如:

components:
  schemas:
    Link:
      type: object
      properties:
        href:
          type: string
        hreflang:
          type: string
        title:
          type: string
        type:
          type: string
        deprecation:
          type: string
        profile:
          type: string
        name:
          type: string
        templated:
          type: boolean
    Links:
      type: object
      discriminator:
        propertyName: _links
      properties:
        _links:
          type: object
          additionalProperties:
            type: string
            $ref: "#/components/schemas/Link"
    CollectionModel:
      type: object
      discriminator:
        propertyName: _embedded
      properties:
        _embedded:
          type: object
        _links:
          type: object
          properties:
            self:
              type: string
            profile:
              type: string
            search:
              type: string
    CollectionModel_Foo:
      type: …
Run Code Online (Sandbox Code Playgroud)

swagger openapi openapi-generator

11
推荐指数
0
解决办法
2398
查看次数

OpenAPI PHP客户端使用anyOf给出致命错误

我正在尝试使用anyOfallOf属性创建OpenAPI自动生成的PHP客户端。

目标是能够返回其中具有多态性的数组:不同类型的对象。
这些对象也具有一个公共的基础对象。

在我的示例模式中,Items是一个数组,其中各项可以是类型ItemOneItemTwo
这两种类型的项目有一个自己的财产(itemOnePropertyitemTwoProperty,分别地),和一个共同的属性baseItemProperty(从继承BaseItemallOf关键字)。

这里有API规范yaml:

openapi: 3.0.0
info:
  title: Test API
  version: 1.0.0
servers:
  - url: https://api.myjson.com/bins

paths:
  /roxgd:
    get:
      operationId: getItems
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Items'
components:
  schemas:

    Items:
      type: array
      items:
        anyOf:
          - $ref: '#/components/schemas/ItemOne'
          - $ref: '#/components/schemas/ItemTwo'

    BaseItem:
      type: object
      properties:
        baseItemProperty:
          type: string

    ItemOne:
      allOf:
        - $ref: '#/components/schemas/BaseItem'
        - type: object …
Run Code Online (Sandbox Code Playgroud)

php polymorphism swagger openapi openapi-generator

10
推荐指数
1
解决办法
231
查看次数

使用 openapi-generator 生成 ktor 服务器存根的正确方法是什么

当尝试从 openapi 为 ktor 生成服务器端存根时,对我来说输出看起来并不是很实用。

在 Github 上设置了一个示例项目,可以在其中查看设置。因为我需要在我的实际项目中使用 maven,所以我在这里也使用了它,但我想这对其他生成方法应该没有影响。

POM 的相关部分是这样的:

<build>
    ...

    <plugins>
        ...

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>resources</id>
                    <phase>compile</phase>
                    <goals><goal>copy-resources</goal></goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

将生成的内容如下:

  • 数据模型
  • Paths.kt包含 ktor-locations 格式的路径路径的对象
  • 将 API 作为扩展函数实现的示例存根 io.ktor.routing.Route
  • 一个完整的应用程序,包括 configs、Dockerfile、Gradle Files 等。

来自 SpringBoot 和 Swagger,我有点困惑,因为如果您想创建一个实现真正业务逻辑的实际应用程序,这组文件并不是很有用。我本来希望一些默认实现包含我只需要实现/覆盖的单个方法。我意识到 ktor 没有内置 DI,所以我也希望必须以routing某种方式扩展我的应用程序中的存根。

但是,有了这组生成的代码,我真的只能使用数据模型和Paths.kt对象。我将拥有自己的应用程序和默认实现,我无法覆盖但需要自己完全重写。

所以我的问题是,如何设置一个合适的 ktor 应用程序实现 openapi 规范。我是否错过了什么,或者我真的只得到模型并Paths.kt与之合作?

编辑

为了更清楚:特别是对于我的应用程序的这些部分,我不高兴没有工具的支持:

fun Application.module(testing: Boolean = false) { …
Run Code Online (Sandbox Code Playgroud)

maven kotlin ktor openapi-generator

10
推荐指数
0
解决办法
1386
查看次数