在Quarkus REST API中,在@requestbody OpenAPI标签中指定多个”implementation=”标签。

huangapple go评论60阅读模式
英文:

Specifying multiple "implementation=" tags in @requestbody openapi tag in quarkus REST api

问题

我有一个Quarkus REST API,它接受JSON字符串(各种类型)作为事件,然后将它们传播到一个KAFKA主题。

我现在想将可能的JSON字符串结构放入OpenAPI合同中(而不必手动键入)。

我已经找到了@requestbody标签,允许我指定一个结构,但我有多种类型的事件,它们都必须发送到同一个Kafka主题。

例如:

@POST
@Path("/student")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Tag(name = "Student Events Service")
@Operation(summary = "Accepts student type events", description = "Accepts student type events")
@APIResponse(responseCode = "200", description = "Event sent successfully")
@APIResponse(responseCode = "400", description = "The JSON packet sent did not conform to any known message or the primary key or shared data in the packet is invalid. \n "
+ "See Response Body for detail. -  "
+ "Error code 400-1 = Bad message structure or unknown message. Log error and continue to next message - "
+ "Error code 400-2 = Bad primary key. Fix data (client or server side) and retry later - "
+ "Error code 400-3 = Invalid shared data. Fix shared data and retry later",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
@APIResponse(responseCode = "500", description = "Unexpected server error. See Response Body for detail. - "
+ "Error code 500-1 = Unable to send to KAFKA, please wait and retry later - "
+ "Error code 500-2 = Unable to connect to verification database, please wait and retry later - "
+ "Error code 500-3 = General server error, Please wait and retry later",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
public Response postStudentEvent(String eventJSON) {

现在,我可以使用以下标签来生成一个类事件JSON字符串的OpenAPI合同:

@RequestBody(
    required = true,
    content = @Content(
      schema = @Schema(implementation = Application.class)
    )
)

但假设我还想能够接受具有CourseEnrollment.class结构的JSON字符串(这些类都是项目的一部分)。

public class CourseEnrollment {
    
  private String eventID;
  private String eventTime;
  private String eventType;
  private String eventUserSource;
  private String externalReference1;
  private String firstName;
  private String lastName;
  private String emailAddressPrimary;
  private String emailAddressAlternate;
  private String periodStartDate;
  private String periodEndDate;
  private String intakeIntakeYearCode;
  private String programmeCode;
}

public class Application   {
  private String eventID;
  private String eventTime;
  private String eventType;
  private String eventUserSource;
  private String eventTrigger;
  
  private ApplicationInfo applicationDetails = null;
  
  private Person personDetails = null;
  
  private School schoolDetails = null;
}

这是一个奇怪的情景,我知道,但这就是我要处理的情况,因为我们有一个外部系统生成事件并将JSON发送到我的REST服务,我必须将JSON传播到本地的KAFKA主题。

谢谢你的建议。

英文:

I have a quarkus REST api that accepts JSON strings (various kinds) as events and then propagates them to a KAFKA topic.

I want to now put the structure of the possible JSON strings in the openAPI contract (without having to type it out myself manually.

I have managed to find the @requestbody tag that allows me to specify the one structure but I have multiple types of events that all have to go to the same Kafka topic.

example:

  @POST
  @Path("/student")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Tag(name = "Student Events Service")
  @Operation(summary = "Accepts student type events", description = "Accepts student type events")
  @APIResponse(responseCode = "200", description = "Event sent successfully")
  @APIResponse(responseCode = "400", description = "The JSON packet sent did not conform to any known message or the primary key or shared data in the packet is invalid. \n "
               + "See Response Body for detail. -  "
               + "Error code 400-1 = Bad message structure or unknown message. Log error and continue to next message - "
               + "Error code 400-2 = Bad primary key. Fix data (client or server side) and retry later - "
               + "Error code 400-3 = Invalid shared data. Fix shared data and retry later",
               content = @Content(mediaType = "application/json",
                                  schema = @Schema(implementation = ErrorResponse.class)))
  @APIResponse(responseCode = "500", description = "Unexpected server error. See Response Body for detail. - "
               + "Error code 500-1 = Unable to send to KAFKA, please wait and retry later - "
               + "Error code 500-2 = Unable to connect to verification database, please wait and retry later - "
               + "Error code 500-3 = General server error, Please wait and retry later",
               content = @Content(mediaType = "application/json",
                                  schema = @Schema(implementation = ErrorResponse.class)))
  public Response postStudentEvent(String eventJSON) {

Now I can put the following tag in to generate the openapi contract for 1 class of eventJSON string

  @RequestBody(
        required = true,
        content = @Content(
          schema = @Schema(implementation = Application.class)
        )
  )

But lets say I also want to be able to accept JSON string with the structure of CourseEnrollment.class (These classes are all part of the project btw).

public class CourseEnrollment {

  private String eventID;
  private String eventTime;
  private String eventType;
  private String eventUserSource;
  private String externalReference1;
  private String firstName;
  private String lastName;
  private String emailAddressPrimary;
  private String emailAddressAlternate;
  private String periodStartDate;
  private String periodEndDate;
  private String intakeIntakeYearCode;
  private String programmeCode;

 public class Application   {
  private String eventID;
  private String eventTime;
  private String eventType;
  private String eventUserSource;
  private String eventTrigger;

  
  private ApplicationInfo applicationDetails = null;

  private Person personDetails = null;

  private School schoolDetails = null;

Its a wierd scenario I know, but thats what I have to deal with as we have an external system generating the events and sending the JSON to my REST service and I have to propagate the JSON to the KAFKA topics locally.

Thank you for your inputs

答案1

得分: 0

你可以使用 oneOf:

  @RequestBody(
        required = true,
        content = @Content(
          schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
        )
  )
英文:

You can use oneOf:

  @RequestBody(
        required = true,
        content = @Content(
          schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
        )
  )

huangapple
  • 本文由 发表于 2023年2月8日 16:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定