[英]OpenAPI returns null in nested type
I have SpringBoot app with Mybatis and Rest API made by OpenAPI Here is my swagger file:我有 SpringBoot 应用程序与 Mybatis 和 Rest API 由 OpenAPI 这是我的 swagger 文件:
/api/v1/getdata:
get:
summary: ttttttt
tags:
- "API"
description: ttttttt
operationId: getdata
parameters:
- name: sap
in: header
required: true
description: ttttttt
schema:
type: string
- name: created
in: header
required: true
description: ttttttt
schema:
type: string
responses:
"200":
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/MyResponse'
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/MyResponse'
components:
schemas:
MyResponse:
description: ttttttt
type: object
properties:
code:
description: ttttttt
type: string
body:
$ref: '#/components/schemas/MyMetaData'
MyMetaData:
description: ttttttt
type: object
properties:
id:
description: id
type: string
somedata:
description: ttttttt
$ref: '#/components/schemas/MyData'
MyData:
description: ttttttt
type: object
properties:
id:
description: id
type: string
group:
description: ttttttt
type: string
Here is my controller code:这是我的 controller 代码:
@Override
public ResponseEntity<MyResponse> getdata(String sap, String created) {
MyResponse myResponse = new MyResponse();
try {
OffsetDateTime date = OffsetDateTime.parse(created, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Optional<MyMetaData> optionalMetaData = Optional.ofNullable(this.MyMetaDataMapper.getdata(sap, date));
if (optionalMetaData.isPresent()) {
myResponse.setBody(optionalMetaData.get());
myResponse.setCode("OK");
}
return ResponseEntity.status(HttpStatus.OK).body(myResponse);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(myResponse);
}
}
Here is generated model:这里生成model:
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2022-03-01T12:24:46.999519+03:00")
public class MyMetaData {
@JsonProperty("id")
private String id = null;
@JsonProperty("somedata")
private MyData somedata = null;
public MyMetaData id(String id) {
this.id = id;
return this;
}
When I use time MyData like this, as a result i get null:当我像这样使用时间 MyData 时,结果我得到 null:
somedata:
description: ttttttt
$ref: '#/components/schemas/MyData'
When somedata is of type string, then I get some normal data from my database.当 somedata 是字符串类型时,我会从我的数据库中获取一些正常数据。 What's the correct way to use object type here?在这里使用 object 类型的正确方法是什么?
The OpenAPI swagger file that you have shared has $ref: '#/components/schemas/MyData
' but components
and schema
is missing in the paths of MyData.您共享的 OpenAPI swagger 文件具有$ref: '#/components/schemas/MyData
' 但 MyData 的路径中缺少components
和schema
。
As per Open API documentation it should be根据 Open API文档,它应该是
components:
schemas:
User: # Schema name
type: object
properties:
id:
type: integer
format: int64
example: 1 # Property example
name:
type: string
example: New order # Property example
For nested objects there is already a StackOverflow question which might help.对于嵌套对象,已经有一个可能有帮助的StackOverflow问题。 Github issue . Github问题。
You can try something similar你可以尝试类似的东西
components:
schemas:
MyMetaData:
type: object
description: ttttttt
properties:
id:
description: id
type: string
somedata:
description: ttttttt
$ref: '#/components/schemas/MyData'
MyData:
type: object
description: ttttttt
properties:
id:
description: id
type: string
If you still face issue with OpenAPI swagger you can try SpringDoc .如果您仍然面临 OpenAPI swagger 的问题,您可以尝试使用 SpringDoc 。 It supports OpenAPI 3+
and generates Swagger documentation easily.它支持OpenAPI 3+
并轻松生成 Swagger 文档。 You can find a lot of examples over web.你可以在 web 上找到很多例子。
Sample: Baeldung OpenAPI Dzone SpringDoc OpenApi示例: Baeldung OpenAPI Dzone SpringDoc OpenApi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.