Skip to content

Replace Page schema with PagedModel when pageSerializationMode is set to VIA_DTO #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
*
* *
* * *
* * * * Copyright 2019-2024 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package org.springdoc.core.configuration;

import org.springdoc.core.converters.PageOpenAPIConverter;
import org.springdoc.core.converters.SortOpenAPIConverter;
import org.springdoc.core.converters.models.SortObject;
import org.springdoc.core.customizers.DataRestDelegatingMethodParameterCustomizer;
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.core.providers.RepositoryRestConfigurationProvider;
import org.springdoc.core.providers.SpringDataWebPropertiesProvider;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PagedModel;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.data.web.config.SpringDataWebSettings;

import java.util.Optional;

import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLED;
import static org.springdoc.core.utils.Constants.SPRINGDOC_SORT_CONVERTER_ENABLED;
import static org.springdoc.core.utils.SpringDocUtils.getConfig;

/**
* The type Spring doc page configuration.
*
* @author Claudio Nave
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
@ConditionalOnClass({ Page.class, PagedModel.class })
@ConditionalOnWebApplication
@ConditionalOnBean(SpringDocConfiguration.class)
public class SpringDocPageConfiguration {

/**
* Page open api converter.
* @param objectMapperProvider the object mapper provider
* @return the page open api converter
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(SpringDataWebSettings.class)
@Lazy(false)
PageOpenAPIConverter pageOpenAPIConverter(SpringDataWebSettings settings,
ObjectMapperProvider objectMapperProvider) {
return new PageOpenAPIConverter(
settings.pageSerializationMode() == EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO,
objectMapperProvider);
}

/**
* Delegating method parameter customizer delegating method parameter customizer.
* @param optionalSpringDataWebPropertiesProvider the optional spring data web
* properties
* @param optionalRepositoryRestConfiguration the optional repository rest
* configuration
* @return the delegating method parameter customizer
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(
Optional<SpringDataWebPropertiesProvider> optionalSpringDataWebPropertiesProvider,
Optional<RepositoryRestConfigurationProvider> optionalRepositoryRestConfiguration) {
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebPropertiesProvider,
optionalRepositoryRestConfiguration);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
*
* *
* * *
* * * * Copyright 2019-2024 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package org.springdoc.core.converters;

import com.fasterxml.jackson.databind.JavaType;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springframework.core.ResolvableType;
import org.springframework.data.web.PagedModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;

/**
* The Spring Data Page type model converter.
*
* @author Claudio Nave
*/
public class PageOpenAPIConverter implements ModelConverter {

private static final String PAGE_TO_REPLACE = "org.springframework.data.domain.Page";

/**
* The constant PAGED_MODEL.
*/
private static final AnnotatedType PAGED_MODEL = new AnnotatedType(PagedModel.class).resolveAsRef(true);

/**
* The Spring doc object mapper.
*/
private final ObjectMapperProvider springDocObjectMapper;
/**
* Flag to replace Page with PagedModel or not.
*/
private final boolean replacePageWithPagedModel;

/**
* Instantiates a new Page open api converter.
* @param replacePageWithPagedModel flag to replace Page with PagedModel or not
* @param springDocObjectMapper the spring doc object mapper
*/
public PageOpenAPIConverter(boolean replacePageWithPagedModel, ObjectMapperProvider springDocObjectMapper) {
this.replacePageWithPagedModel = replacePageWithPagedModel;
this.springDocObjectMapper = springDocObjectMapper;
}

/**
* Resolve schema.
* @param type the type
* @param context the context
* @param chain the chain
* @return the schema
*/
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (replacePageWithPagedModel && PAGE_TO_REPLACE.equals(cls.getCanonicalName())) {
if (!type.isSchemaProperty())
type = resolvePagedModelType(type);
else
type.name(cls.getSimpleName() + StringUtils.capitalize(type.getParent().getType()));
}
}
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
}

private AnnotatedType resolvePagedModelType(AnnotatedType type) {
Type pageType = type.getType();
if (pageType instanceof ParameterizedType) {
Type argumentType = ((ParameterizedType) type.getType()).getActualTypeArguments()[0];
Type pagedModelType = ResolvableType
.forClassWithGenerics(PagedModel.class, ResolvableType.forType(argumentType))
.getType();
return new AnnotatedType(pagedModelType).resolveAsRef(true);
}
else {
return PAGED_MODEL;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ org.springdoc.core.configuration.SpringDocFunctionCatalogConfiguration
org.springdoc.core.configuration.SpringDocHateoasConfiguration
org.springdoc.core.configuration.SpringDocPageableConfiguration
org.springdoc.core.configuration.SpringDocSortConfiguration
org.springdoc.core.configuration.SpringDocPageConfiguration
org.springdoc.core.configuration.SpringDocSpecPropertiesConfiguration
org.springdoc.core.configuration.SpringDocDataRestConfiguration
org.springdoc.core.configuration.SpringDocKotlinConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,68 @@
}
}
},
"/application/sbom": {
"get": {
"operationId": "sbom",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
},
"application/vnd.spring-boot.actuator.v2+json": {
"schema": {
"type": "object"
}
},
"application/vnd.spring-boot.actuator.v3+json": {
"schema": {
"type": "object"
}
}
},
"description": "OK"
}
},
"summary": "Actuator web endpoint 'sbom'",
"tags": [
"Actuator"
]
}
},
"/application/sbom/{id}": {
"get": {
"operationId": "sbom-id",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/octet-stream": {
"schema": {
"type": "object"
}
}
},
"description": "OK"
}
},
"summary": "Actuator web endpoint 'sbom-id'",
"tags": [
"Actuator"
]
}
},
"/application/metrics": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,68 @@
}
}
},
"/application/sbom": {
"get": {
"operationId": "sbom",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
},
"application/vnd.spring-boot.actuator.v2+json": {
"schema": {
"type": "object"
}
},
"application/vnd.spring-boot.actuator.v3+json": {
"schema": {
"type": "object"
}
}
},
"description": "OK"
}
},
"summary": "Actuator web endpoint 'sbom'",
"tags": [
"Actuator"
]
}
},
"/application/sbom/{id}": {
"get": {
"operationId": "sbom-id",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/octet-stream": {
"schema": {
"type": "object"
}
}
},
"description": "OK"
}
},
"summary": "Actuator web endpoint 'sbom-id'",
"tags": [
"Actuator"
]
}
},
"/application/metrics": {
"get": {
"tags": [
Expand Down
Loading