我想使用openapi-generator-maven-plugin中的typemappings。我的openapi规范文件看起来像下面这样:
paths:
/maintenance-windows-avr/{day}:
get:
summary: "Get active maintenance windows for day"
tags:
- Maintenance Windows AVR
operationId: getMaintenanceWindows
parameters:
- in: path
name: day
required: true
schema:
type: string
format: date
description: day to return active maintenance windows
responses:
"200":
description: "Success"
content:
application/json:
schema:
type: object
items:
$ref: "#/components/schemas/ResponseListMaintenanceWindowAVRDto"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorDto"
components:
schemas:
ErrorDto:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
MaintenanceWindowAVRDto:
type: object
required:
- id
- start
- end
- hosts
properties:
id:
type: string
format: uuid
description: id of maintenance window
start:
type: string
format: date-time
description: when the maintenance window is going to start
end:
type: string
format: date-time
description: when the maintenance is going to end
hosts:
type: array
items:
type: long
format: int64
description: list of host ids
ResponseListMaintenanceWindowAVRDto:
type: object
properties:
data:
type: array
items:
$ref: "#/components/schemas/MaintenanceWindowAVRDto"
这将生成如下所示的文件MaintenanceWindowsAvrApi和MaintenanceWindowsAVRDto:
package eu.nts.nmp.csm.mac.api.generated.endpoint.avl;
import eu.nts.nmp.csm.mac.api.generated.model.avl.ErrorDto;
import java.time.LocalDate;
import eu.nts.nmp.csm.mac.api.generated.model.avl.ResponseListMaintenanceWindowAVRDto;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.Map;
import java.util.List;
import javax.validation.constraints.*;
import javax.validation.Valid;
@Path("/maintenance-windows-avr/{day}")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2023-03-14T16:46:37.723499+01:00[Europe/Vienna]")public interface MaintenanceWindowsAvrApi {
@GET
@Produces({ "application/json" })
Response getMaintenanceWindows(@PathParam("day") LocalDate day);
}
package eu.nts.nmp.csm.mac.api.generated.model.avl;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.validation.constraints.*;
import javax.validation.Valid;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2023-03-14T16:46:37.723499+01:00[Europe/Vienna]")public class MaintenanceWindowAVRDto {
private @Valid UUID id;
private @Valid OffsetDateTime start;
private @Valid OffsetDateTime end;
private @Valid List<Long> hosts = new ArrayList<>();
/**
* id of maintenance window
**/
public MaintenanceWindowAVRDto id(UUID id) {
this.id = id;
return this;
}
@JsonProperty("id")
@NotNull
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
/**
* when the maintenance window is going to start
**/
public MaintenanceWindowAVRDto start(OffsetDateTime start) {
this.start = start;
return this;
}
@JsonProperty("start")
@NotNull
public OffsetDateTime getStart() {
return start;
}
public void setStart(OffsetDateTime start) {
this.start = start;
}
/**
* when the maintenance is going to end
**/
public MaintenanceWindowAVRDto end(OffsetDateTime end) {
this.end = end;
return this;
}
@JsonProperty("end")
@NotNull
public OffsetDateTime getEnd() {
return end;
}
public void setEnd(OffsetDateTime end) {
this.end = end;
}
/**
**/
public MaintenanceWindowAVRDto hosts(List<Long> hosts) {
this.hosts = hosts;
return this;
}
@JsonProperty("hosts")
@NotNull
public List<Long> getHosts() {
return hosts;
}
public void setHosts(List<Long> hosts) {
this.hosts = hosts;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MaintenanceWindowAVRDto maintenanceWindowAVRDto = (MaintenanceWindowAVRDto) o;
return Objects.equals(this.id, maintenanceWindowAVRDto.id) &&
Objects.equals(this.start, maintenanceWindowAVRDto.start) &&
Objects.equals(this.end, maintenanceWindowAVRDto.end) &&
Objects.equals(this.hosts, maintenanceWindowAVRDto.hosts);
}
@Override
public int hashCode() {
return Objects.hash(id, start, end, hosts);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class MaintenanceWindowAVRDto {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" start: ").append(toIndentedString(start)).append("\n");
sb.append(" end: ").append(toIndentedString(end)).append("\n");
sb.append(" hosts: ").append(toIndentedString(hosts)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
我想将MaintenanceWindowsAvrApi文件中的LocalDate参数和从MaintenanceWindowsAVRDto文件开始和结束的两个OffsetDateTime字段Map到一个Instant(java.time.Instant)。
我尝试了很多方法,但没有一个能给我想要的结果。我的API pom文件:
<inputSpec>${project.basedir}/src/main/resources/META-INF/openapi-avr.yaml</inputSpec>
<generatorName>jaxrs-spec</generatorName>
<configOptions>
<useSwaggerAnnotations>false</useSwaggerAnnotations>
<interfaceOnly>true</interfaceOnly>
<generatePom>false</generatePom>
<returnResponse>true</returnResponse>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<useFlags>true</useFlags>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
如何使用typeMappings解决我的问题?
1条答案
按热度按时间qij5mzcb1#
解决方案
像这样添加typeMappings配置:
这将用
java.time.Instant
替换生成代码中出现的LocalDateTime
和OffsetDateTime
。导入部分不会更改。也就是说,它们仍然包含import java.time.LocalDate;
和import java.time.OffsetDateTime;
,即使这些导入不再使用。关于typeMappings的文档可以在这里和这里找到。
OpenAPI生成器Bug
OpenAPI Generator的预期用途是设置
和
(And对于OffsetDateTime也是如此。)
然后,
LocalDate
和OffsetDateTime
的导入将被import java.time.Instant;
替换,代码中将使用Instant
而不是java.time.Instant
。然而,OpenAPI Generator中有一个bug,它阻止在基于Java的生成器中使用
importMapping
。因此,我们目前需要配置类型Map,如我上面的解决方案所示。未知配置选项
顺便说一下,我认为可以删除代码中的
useFlags
设置,因为这不是OpenAPI Generator的有效配置选项。