JsonIgnore 使用开放 API 规范

问题描述 投票:0回答:2

我使用 OpenAPI 规范来生成 Java POJO。我需要在 Open API yaml 中指定什么才能生成以下 POJO 的等效项?

...
@JsonIgnore
public String ignoredProperty;
...

我的 yaml 规范如下

openapi: 3.0.0
info:
  title: Cool API
  description: A Cool API spec
  version: 0.0.1
servers:
  - url: http://api.cool.com/v1
    description: Cool server for testing
paths:
  /
  ...
components:
  schemas:
    MyPojo:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        # I want the below attribute to be ignored as a part of JSON 
        ignoreProperty:  
          type: string  
java swagger openapi openapi-generator
2个回答
7
投票

openapi generator
支持供应商扩展。具体来说,对于 Java 生成器,截至撰写本文时它支持以下扩展。不过,可以在此处找到最新列表。

扩展名 描述 适用于 默认值
x-鉴别器值 与模型继承一起使用,为标识当前模型的鉴别器指定值 型号
x 工具 能够指定模型必须实现的接口 型号 空数组
x-setter-额外注释 可以通过 java setter 为特定字段指定自定义注释 当字段为数组和uniqueItems时,此扩展用于在setter上添加
@JsonDeserialize(as = LinkedHashSet.class)
,否则无值
x 标签 指定多个swagger标签进行操作 操作
x-接受 为操作指定“接受”标头的自定义值 操作
x-内容类型 为操作指定“Content-Type”标头的自定义值 操作
x-class-extra-annotation 要添加到模型的自定义注释列表 型号
x 字段额外注释 要添加到属性的自定义注释列表
x-webclient-阻止 指定特定操作的方法应该是阻塞还是非阻塞(例如:在生成的方法中返回
Mono<T>/Flux<T>
return T/List<T>/Set<T>
并执行
.block()
操作

您可以使用上面列出的

x-field-extra-annotation
供应商扩展向任何字段添加注释。因此,对于您的示例,您可以添加以下内容:

openapi: 3.0.0
info:
  title: Cool API
  description: A Cool API spec
  version: 0.0.1
servers:
  - url: http://api.cool.com/v1
    description: Cool server for testing
paths:
  /
  ...
components:
  schemas:
    MyPojo:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        # I want the below attribute to be ignored as a part of JSON 
        ignoreProperty:  
          type: string  
          x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonIgnore"

0
投票

我今天遇到了同样的问题,截至今天的当前答案对我来说不再适用于 spring (具有相同的供应商扩展),因为看起来同时 jackson 更改了方法访问器 JSONProperty (在 getter 级别生成)覆盖的行为Json从字段级别忽略。

但是,通过在字段级别上添加以下注释,我可以使其工作:

x-field-extra-annotation: "@com.fasterxml.jackson.annotation.JsonProperty(access = JsonProperty.Access.WRITE_ONLY)"
© www.soinside.com 2019 - 2024. All rights reserved.