简体   繁体   English

Swagger Codegen 3和Spring HATEOAS

[英]Swagger Codegen 3 and Spring HATEOAS

I'm taking my first swing at creating a RESTful API with OAS 3.0, Swagger Codegen, Spring MVC, and Spring HATEOAS. 我正在用OAS 3.0,Swagger Codegen,Spring MVC和Spring HATEOAS创建RESTful API的第一步。 I want to use the OAS definition for documentation generation and server/client stub generation and use HATEOAS to create hyperlinks related resources. 我想将OAS定义用于文档生成和服务器/客户端存根生成,并使用HATEOAS创建与超链接相关的资源。

I currently have my resources extending ResourceSupport and can add my links such that the responses have the _embedded and _links fields that I would expect. 我目前拥有扩展ResourceSupport并且可以添加链接,以便响应具有我期望的_embedded_links字段。 My issue is how to properly map the HATEOAS Resource to the model generated by Swagger codegen. 我的问题是如何正确地将HATEOAS Resource映射到Swagger代码生成的模型。 My OAS definition matches the hal+json response, so the fields are identical in the swagger model and my HATEOAS Response . 我的OAS定义与hal+json响应匹配,因此在swagger模型和HATEOAS Response ,这些字段相同。

Is there a way to easily map these? 有没有一种方法可以轻松地映射这些? I'm also willing to accept that I am interpreting this incorrectly and that these frameworks don't really mesh together. 我也愿意接受我误解了这些框架,并且这些框架并没有真正融合在一起。

OAS example: OAS示例:

responses:
  200:
    description: ...
    content:
      application/hal+json:
        schema:
          $ref: '#/components/schemas/OasPersonResponse'

components:
  schemas:
    OasPersonResponse:
      type: object
          properties:
            firstName:
              type: string
            lastName:
              type: string
            _links:
              type: object
              properties:
                self:
                  type: object
                  properties:                  
                    href:
                    type: string

Resource example: Resource示例:

public class PersonResource extends ResourceSupport {
  private final Person person;

  public PersonResource(Person person) {
    this.person = person;
  }

  public String getFirstName() {
    return person.getFirstName();
  }

  public String getLastName() {
    return person.getLastName();
  }
}

Controller Example: 控制器示例:

@Controller
public class PersonController implements PersonApi {

  @Override
  public ResponseEntity<OasPersonResponse> getPersonById(Integer personId) {
    Person person = someDb.getPerson(personId);
    PersonResource personResource = new PersonResource(person);

    personResource
      .add(linkTo(methodOn(PersonController.class)
        .getPersonById(personId))
      .withSelfRel();

    Resource<PersonResource> returnResource = 
      new Resource(personResource);

    return new ResponseEntity<>(returnResponse, HttpStatus.OK);
}

My issue is with the stub generated by swagger codegen expecting a return type of ResponseEntity<OasPersonResponse> but have a reference to a Resource<PersonResource> . 我的问题是由swagger ResponseEntity<OasPersonResponse>生成的存根,期望返回类型为ResponseEntity<OasPersonResponse>但具有对Resource<PersonResource>的引用。 Both OasPersonResponse and PersonResource represent the same data but the OasPersonResponse explicitly defines the _links object whereas the response with the PersonResource gets serialized to have the _links object. OasPersonResponsePersonResource代表相同的数据,但是OasPersonResponse显式定义了_links对象,而PersonResource的响应被序列化为_links对象。

Is there an easy way for me to convert the HATEOAS Resource to the model that was created by swagger codegen? 我有一种简单的方法可以将HATEOAS资源转换为由swagger代码生成的模型吗?

Thanks in advance for the help and guidance. 在此先感谢您的帮助和指导。

I'm currently working on a very similar project! 我目前正在从事一个非常相似的项目! Firstly, if you can, I'd recommend using the 1.0.0.RC1 version of spring-hateoas as it has some pretty major quality of life improvements over the 0.25.x release branch. 首先,如果可以的话,我建议使用spring-hateoas的1.0.0.RC1版本,因为它比0.25.x版本分支具有相当大的生活质量改善。 Of major relevance is that using the EntityModel wrapper class is now the recommended practice, which means you can just leave the relations out of your base entity specification. 最重要的是,现在推荐使用EntityModel包装器类,这意味着您可以将这些关系排除在基本实体规范之外。 (The downside is this reduces the immediate utility of the OpenAPI spec; I haven't quite figured out how to reconcile that yet.) (缺点是这减少了OpenAPI规范的直接实用性;我还没有弄清楚如何协调它。)

Secondly, I'm afraid there doesn't seem to be much existing work on the swagger-codegen side as far as supporting Spring HATEOAS is concerned; 其次,就支持Spring HATEOAS而言,恐怕在swagger-codegen方面似乎没有太多现有工作。 in fact, I keep running into annoying bugs with the plain Spring "language" generator. 实际上,我一直在使用普通的Spring“语言”生成器遇到烦人的错误。

So either we can write our own swagger-codegen generator for spring-hateoas, or just heavily customize some templates to get "close enough" (there's less of this needed when using the EntityModel wrapper rather than extending ResourceSupport ). 因此,我们既可以编写自己的swagger-codegen生成器来处理spring-hateoas,也可以仅大量定制一些模板以使其“足够接近”(使用EntityModel包装器而不是扩展ResourceSupport时,所需的内容就更少了)。 I've gone with the latter approach so far, for what that's worth. 到目前为止,我已经采用了后一种方法,这是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM