当前位置: 首页 > 知识库问答 >
问题:

如何为静态编程语言数据类中的属性设置OpenAPI类型/模式

许嘉福
2023-03-14

在使用Kotlin的微文件/Quarkus项目中,有一个带有Instant类型变量的数据类。

@Schema(name = "Vehicle", description = "POJO that represents a vehicle at a specific time.")
data class VehicleDTO(
    var time: Instant = Instant.EPOCH
)

问题是,生成的openapi模式并不表示Instant的值实际上是如何传输的。

模式如下所示,而它仅表示为这样的字符串:2015-06-02T21:34:33.616Z

Instant:
  type: object
  properties:
    nanos:
      format: int32
      type: integer
    seconds:
      format: int64
      type: integer
    epochSecond:
      format: int64
      type: integer
    nano:
      format: int32
      type: integer

我已经尝试对数据类进行注释,以使用实现字符串和类型字符串,但它没有改变任何东西。

@Schema(name = "Vehicle", description = "POJO that represents a vehicle at a specific time.")
data class VehicleDTO(
    @Schema(implementation = String::class, type = SchemaType.STRING)
    var time: Instant = Instant.EPOCH
)

共有1个答案

潘宸
2023-03-14

问题是数据类受到了一些特殊处理,并且您的注释放置在构造函数参数上。

您可以在数据类生成的Java代码中看到这一点。相关片段:

@Schema(
   name = "Vehicle",
   description = "POJO that represents a vehicle at a specific time."
)
@Metadata(...)
public final class VehicleDTO {
   @NotNull
   private Instant time;

   @NotNull
   public final Instant getTime() {
      return this.time;
   }

   public final void setTime(@NotNull Instant var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.time = var1;
   }

   public VehicleDTO(@Schema(implementation = String.class,type = SchemaType.STRING) @NotNull Instant time) {
      Intrinsics.checkParameterIsNotNull(time, "time");
      super();
      this.time = time;
   }

   // ...
}

您需要告诉静态编程语言使用使用站点目标将其放置在字段中:

@Schema(name = "Vehicle", description = "POJO that represents a vehicle at a specific time.")
data class VehicleDTO(
    @field:Schema(implementation = String::class, type = SchemaType.STRING)
    var time: Instant = Instant.EPOCH
)

后续相关代码:

@Schema(
   name = "Vehicle",
   description = "POJO that represents a vehicle at a specific time."
)
@Metadata(...)
public final class VehicleDTO {
   @Schema(
      implementation = String.class,
      type = SchemaType.STRING
   )
   @NotNull
   private Instant time;

   @NotNull
   public final Instant getTime() {
      return this.time;
   }

   public final void setTime(@NotNull Instant var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.time = var1;
   }

   public VehicleDTO(@NotNull Instant time) {
      Intrinsics.checkParameterIsNotNull(time, "time");
      super();
      this.time = time;
   }

   // ...
}
 类似资料:
  • 是否有可能在Kotlin 属性初始化之前从其获取< code>::class.java? 从逻辑上讲,它应该可以工作——我试图获取一个类而不是一个值,但实际上它在未初始化的属性访问异常时失败。 请注意,我试图获取类的属性位于泛型类中,其类型是泛型参数之一: 我需要这个类来创建的实例 当然,我不能做到: 对此有什么解决办法吗?

  • 我是Kotlin的初学者,我实际上正在Kotlin中做一个Androïd应用程序。我必须初始化一些值未知的属性(它不是,但从现在开始很难定义),所以我想像在TypeScript中一样,例如:这意味着startDate的类型可以是任何东西(它不受保护,但它有助于获得值,而不是关于发生了什么)。 有没有办法在科特林做到这一点?我试过了或者如列表中的* 感谢您的阅读!

  • 本文向大家介绍动态语言、动态类型语言、静态类型语言、强类型语言、弱类型语言介绍,包括了动态语言、动态类型语言、静态类型语言、强类型语言、弱类型语言介绍的使用技巧和注意事项,需要的朋友参考一下 关于如题这几个概念,大部分人应该比较熟悉,但是我昏了好久,所以写下来加深印象。 1. 动态语言 又叫动态编程语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化。比如

  • 是否可以在中的class中添加一个新的静态方法?通常,由于Kotlin Extensions,这样的事情在Kotlin中是可能的。 我已经尝试在一个名为的文件中执行以下操作: 但<code>数学。无法解析同伴。。。

  • 我读过关于Kotlin数据类的书,认为它们在描述数据传输对象(DTO)的情况下非常有用。在我的Java项目中,我已经有了用Java编写的DTO类,比如: 这些DTO类存储在单独的工件中,我将其作为依赖项添加到其他工件中。所以,我决定用Kotlin类替换它,并在Kotlin上重写了提到的Tweet类,所以它开始看起来像: 这是我第一次使用静态编程语言,所以可能有些东西看起来很难看,但我的主要问题是—

  • 函数应将类型擦除列表转换为,即