gpt4 book ai didi

java - Swagger Codegen 将 "format: time"转换为非字符串

转载 作者:行者123 更新时间:2023-11-30 06:06:15 26 4
gpt4 key购买 nike

我正在尝试自定义 OpenAPI 规范字符串格式之间的映射

  • 时间
  • 日期
  • 日期时间

  • 偏移时间
  • 本地日期
  • 即时

分别。

我目前正在使用自己的自定义服务器生成器来扩展 JavaJerseyServerCodegen,因此我进行了此更改并且一切正常

@Override
public void processOpts()
{
super.processOpts();
typeMapping.put("DateTime", "Instant");
typeMapping.put("date", "LocalDate");

importMapping.put("Instant", "java.time.Instant");
importMapping.put("LocalDate", "java.time.LocalDate");
...

问题是时间格式,因为代码生成器默认没有定义这种格式,因此我可以“覆盖”它。
可以做我想做的事吗?如果是这样怎么办?

最佳答案

经过一番努力之后,我尝试了 KISS 方法并得到了以下解决方案:

@Override
public String getSwaggerType(final Property property)
{
if ((property instanceof StringProperty) && ("time".equals(property.getFormat())))
{
return "OffsetTime";
}
else
{
return super.getSwaggerType(property);
}
}

现在我可以简单地执行以下操作

@Override
public void processOpts()
{
super.processOpts();
typeMapping.put("DateTime", "Instant");
typeMapping.put("date", "LocalDate");
typeMapping.put("time", "OffsetTime");

importMapping.put("Instant", "java.time.Instant");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("OffsetTime", "java.time.OffsetTime");
...

关于java - Swagger Codegen 将 "format: time"转换为非字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com