Skip to content

Support Spring Data Sort and @SortDefault #1702

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -97,6 +97,11 @@ public final class Constants {
*/
public static final String SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED = "springdoc.model-converters.polymorphic-converter.enabled";

/**
* The constant SPRINGDOC_SORT_CONVERTER_ENABLED.
*/
public static final String SPRINGDOC_SORT_CONVERTER_ENABLED = "springdoc.model-converters.sort-converter.enabled";

/**
* The constant SPRINGDOC_SCHEMA_RESOLVE_PROPERTIES.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ public static class ModelConverters {
*/
private PolymorphicConverter polymorphicConverter = new PolymorphicConverter();

/**
* The Sort converter.
*/
private SortConverter sortConverter = new SortConverter();

/**
* Gets deprecating converter.
Expand Down Expand Up @@ -772,6 +776,24 @@ public void setPolymorphicConverter(PolymorphicConverter polymorphicConverter) {
this.polymorphicConverter = polymorphicConverter;
}

/**
* Gets sort converter.
*
* @return the sort converter
*/
public SortConverter getSortConverter() {
return sortConverter;
}

/**
* Sets sort converter.
*
* @param sortConverter the sort converter
*/
public void setSortConverter(SortConverter sortConverter) {
this.sortConverter = sortConverter;
}

/**
* The type Deprecating converter.
* @author bnasslashen
Expand Down Expand Up @@ -861,6 +883,36 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

/**
* The type Sort converter.
* @author daniel-shuy
*/
public static class SortConverter {

/**
* The Enabled.
*/
private boolean enabled;

/**
* Is enabled boolean.
*
* @return the boolean
*/
public boolean isEnabled() {
return enabled;
}

/**
* Sets enabled.
*
* @param enabled the enabled
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.springdoc.core.converters.PropertyCustomizingConverter;
import org.springdoc.core.converters.ResponseSupportConverter;
import org.springdoc.core.converters.SchemaPropertyDeprecatingConverter;
import org.springdoc.core.converters.SortOpenAPIConverter;
import org.springdoc.core.customizers.ActuatorOpenApiCustomizer;
import org.springdoc.core.customizers.ActuatorOperationCustomizer;
import org.springdoc.core.customizers.DataRestDelegatingMethodParameterCustomizer;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -107,6 +109,7 @@
import static org.springdoc.core.Constants.SPRINGDOC_SCHEMA_RESOLVE_PROPERTIES;
import static org.springdoc.core.Constants.SPRINGDOC_SHOW_ACTUATOR;
import static org.springdoc.core.Constants.SPRINGDOC_SHOW_SPRING_CLOUD_FUNCTIONS;
import static org.springdoc.core.Constants.SPRINGDOC_SORT_CONVERTER_ENABLED;
import static org.springdoc.core.SpringDocUtils.getConfig;

/**
Expand Down Expand Up @@ -537,6 +540,42 @@ DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional
}
}

/**
* The type Spring doc sort configuration.
*/
@ConditionalOnClass(Sort.class)
static class SpringDocSortConfiguration {

/**
* Sort open api converter.
*
* @param objectMapperProvider the object mapper provider
* @return the sort open api converter
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = SPRINGDOC_SORT_CONVERTER_ENABLED, matchIfMissing = true)
@Lazy(false)
SortOpenAPIConverter sortOpenAPIConverter(ObjectMapperProvider objectMapperProvider) {
getConfig().replaceParameterObjectWithClass(org.springframework.data.domain.Sort.class, org.springdoc.core.converters.models.Sort.class);
return new SortOpenAPIConverter(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);
}
}

/**
* The type Spring doc spring data web properties provider.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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 java.util.Iterator;

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.converters.models.Sort;
import org.springdoc.core.providers.ObjectMapperProvider;

/**
* The Spring Data Sort type model converter.
* @author daniel-shuy
*/
public class SortOpenAPIConverter implements ModelConverter {

private static final String SORT_TO_REPLACE = "org.springframework.data.domain.Sort";

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

/**
* The Spring doc object mapper.
*/
private final ObjectMapperProvider springDocObjectMapper;

/**
* Instantiates a new Sort open api converter.
*
* @param springDocObjectMapper the spring doc object mapper
*/
public SortOpenAPIConverter(ObjectMapperProvider springDocObjectMapper) {
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 (SORT_TO_REPLACE.equals(cls.getCanonicalName())) {
if (!type.isSchemaProperty())
type = SORT;
else
type.name(cls.getSimpleName() + StringUtils.capitalize(type.getParent().getType()));
}
}
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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.models;

import java.util.List;
import java.util.Objects;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;

/**
* The Sort type.
* @author daniel-shuy
*/
public class Sort {

/**
* The Sort.
*/
@Parameter(description = "Sorting criteria in the format: property,(asc|desc). "
+ "Default sort order is ascending. " + "Multiple sort criteria are supported."
, name = "sort"
, array = @ArraySchema(schema = @Schema(type = "string")))
private List<String> sort;

/**
* Instantiates a new Sort.
*
* @param sort the sort
*/
public Sort(List<String> sort) {
this.sort = sort;
}

/**
* Gets sort.
*
* @return the sort
*/
public List<String> getSort() {
return sort;
}

/**
* Sets sort.
*
* @param sort the sort
*/
public void setSort(List<String> sort) {
if (sort == null) {
this.sort.clear();
}
else {
this.sort = sort;
}
}

/**
* Add sort.
*
* @param sort the sort
*/
public void addSort(String sort) {
this.sort.add(sort);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Sort sort = (Sort) o;
return Objects.equals(this.sort, sort.sort);
}

@Override
public int hashCode() {
return Objects.hash(sort);
}

@Override
public String toString() {
return "Sort{" +
"sort=" + sort +
'}';
}
}
Loading