Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* The Spring Data Page type model converter.
*
* @author Claudio Nave
* @author dpkass
*/
public class PageOpenAPIConverter implements ModelConverter {

Expand Down Expand Up @@ -77,6 +78,15 @@ public class PageOpenAPIConverter implements ModelConverter {
"empty"
);

private static final List<String> PAGED_MODEL_REQUIRED_PROPERTIES = List.of("content", "page");

private static final List<String> PAGE_METADATA_REQUIRED_PROPERTIES = List.of(
"size",
"number",
"totalElements",
"totalPages"
);

/**
* The Spring doc object mapper.
*/
Expand Down Expand Up @@ -110,9 +120,11 @@ public PageOpenAPIConverter(boolean replacePageWithPagedModel, ObjectMapperProvi
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
boolean isPageType = false;
boolean isPagedModelType = false;
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
isPageType = PAGE_TO_REPLACE.equals(cls.getCanonicalName());
isPagedModelType = PagedModel.class.isAssignableFrom(cls);
if (replacePageWithPagedModel && isPageType) {
if (!type.isSchemaProperty())
type = resolvePagedModelType(javaType, type);
Expand All @@ -124,6 +136,8 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato

if (isPageType && !replacePageWithPagedModel)
sortPageSchemaProperties(schema, context);
else if (isPagedModelType || isPageType)
requirePagedModelProperties(schema, context);
return schema;
}

Expand Down Expand Up @@ -181,6 +195,29 @@ private void sortPageSchemaProperties(Schema schema, ModelConverterContext conte
pageSchema.setProperties(sortedProperties);
}

/**
* Require properties emitted by Spring Data's stable page representation.
*
* @param schema the schema
* @param context the context
*/
private void requirePagedModelProperties(Schema schema, ModelConverterContext context) {
Schema pagedModelSchema = resolveReferencedSchema(schema, context);
if (pagedModelSchema == null || pagedModelSchema.getProperties() == null
|| !pagedModelSchema.getProperties().keySet().containsAll(PAGED_MODEL_REQUIRED_PROPERTIES))
return;

pagedModelSchema.setRequired(PAGED_MODEL_REQUIRED_PROPERTIES);

Schema metadataSchema = resolveReferencedSchema(
(Schema) pagedModelSchema.getProperties().get("page"), context);
if (metadataSchema == null || metadataSchema.getProperties() == null
|| !metadataSchema.getProperties().keySet().containsAll(PAGE_METADATA_REQUIRED_PROPERTIES))
return;

metadataSchema.setRequired(PAGE_METADATA_REQUIRED_PROPERTIES);
}

/**
* Resolve referenced schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.web.config.EnableSpringDataWebSupport;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
Expand All @@ -47,6 +48,10 @@ protected void testApp() throws Exception {
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
.andExpect(status().isOk())
.andExpect(jsonPath("$.openapi", is("3.1.0")))
.andExpect(jsonPath("$.components.schemas.PagedModelString.required",
containsInAnyOrder("content", "page")))
.andExpect(jsonPath("$.components.schemas.PageMetadata.required",
containsInAnyOrder("size", "number", "totalElements", "totalPages")))
.andExpect(content().json(getContent("results/3.1.0/app10-direct.json"), true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@
"type": "integer",
"format": "int64"
}
}
},
"required": [
"number",
"size",
"totalElements",
"totalPages"
]
},
"PagedModelString": {
"type": "object",
Expand All @@ -227,7 +233,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PagedModel": {
"type": "object",
Expand All @@ -241,7 +251,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"DummyListString": {
"type": "object",
Expand All @@ -266,7 +280,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PageString": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@
"type": "integer",
"format": "int64"
}
}
},
"required": [
"number",
"size",
"totalElements",
"totalPages"
]
},
"PagedModelString": {
"type": "object",
Expand All @@ -227,7 +233,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PagedModel": {
"type": "object",
Expand All @@ -241,7 +251,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"DummyListString": {
"type": "object",
Expand All @@ -266,7 +280,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PagedModelUserDto": {
"type": "object",
Expand All @@ -280,7 +298,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"UserDto": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@
"type": "integer",
"format": "int64"
}
}
},
"required": [
"number",
"size",
"totalElements",
"totalPages"
]
},
"PagedModelString": {
"type": "object",
Expand All @@ -207,7 +213,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PagedModel": {
"type": "object",
Expand All @@ -219,7 +229,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"DummyListString": {
"type": "object",
Expand All @@ -244,7 +258,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PageString": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@
"type": "integer",
"format": "int64"
}
}
},
"required": [
"number",
"size",
"totalElements",
"totalPages"
]
},
"PagedModelString": {
"type": "object",
Expand All @@ -207,7 +213,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"PagedModel": {
"type": "object",
Expand All @@ -219,7 +229,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"DummyListString": {
"type": "object",
Expand All @@ -244,7 +258,11 @@
"page": {
"$ref": "#/components/schemas/PageMetadata"
}
}
},
"required": [
"content",
"page"
]
},
"DummyPageString": {
"type": "object",
Expand Down
Loading