Skip to content

Commit 36b0518

Browse files
committed
Changes report: #1668
1 parent a7bd107 commit 36b0518

File tree

26 files changed

+1734
-36
lines changed

26 files changed

+1734
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package org.springdoc.core.configuration;
26+
27+
import java.util.Optional;
28+
29+
import org.springdoc.core.converters.SortOpenAPIConverter;
30+
import org.springdoc.core.customizers.DataRestDelegatingMethodParameterCustomizer;
31+
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
32+
import org.springdoc.core.providers.ObjectMapperProvider;
33+
import org.springdoc.core.providers.RepositoryRestConfigurationProvider;
34+
import org.springdoc.core.providers.SpringDataWebPropertiesProvider;
35+
36+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
37+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
38+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
39+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
40+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
41+
import org.springframework.context.annotation.Bean;
42+
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.context.annotation.Lazy;
44+
import org.springframework.data.domain.Sort;
45+
46+
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLED;
47+
import static org.springdoc.core.utils.Constants.SPRINGDOC_SORT_CONVERTER_ENABLED;
48+
import static org.springdoc.core.utils.SpringDocUtils.getConfig;
49+
50+
/**
51+
* The type Spring doc pageable configuration.
52+
* @author bnasslahsen
53+
*/
54+
@Lazy(false)
55+
@Configuration(proxyBeanMethods = false)
56+
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
57+
@ConditionalOnClass(Sort.class)
58+
@ConditionalOnWebApplication
59+
@ConditionalOnBean(SpringDocConfiguration.class)
60+
public class SpringDocSortConfiguration {
61+
62+
/**
63+
* Sort open api converter.
64+
*
65+
* @param objectMapperProvider the object mapper provider
66+
* @return the sort open api converter
67+
*/
68+
@Bean
69+
@ConditionalOnMissingBean
70+
@ConditionalOnProperty(name = SPRINGDOC_SORT_CONVERTER_ENABLED, matchIfMissing = true)
71+
@Lazy(false)
72+
SortOpenAPIConverter sortOpenAPIConverter(ObjectMapperProvider objectMapperProvider) {
73+
getConfig().replaceParameterObjectWithClass(org.springframework.data.domain.Sort.class, org.springdoc.core.converters.models.Sort.class);
74+
return new SortOpenAPIConverter(objectMapperProvider);
75+
}
76+
77+
/**
78+
* Delegating method parameter customizer delegating method parameter customizer.
79+
*
80+
* @param optionalSpringDataWebPropertiesProvider the optional spring data web properties
81+
* @param optionalRepositoryRestConfiguration the optional repository rest configuration
82+
* @return the delegating method parameter customizer
83+
*/
84+
@Bean
85+
@ConditionalOnMissingBean
86+
@Lazy(false)
87+
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional<SpringDataWebPropertiesProvider> optionalSpringDataWebPropertiesProvider, Optional<RepositoryRestConfigurationProvider> optionalRepositoryRestConfiguration) {
88+
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebPropertiesProvider, optionalRepositoryRestConfiguration);
89+
}
90+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.springdoc.core.converters;
2+
3+
/*
4+
*
5+
* *
6+
* * *
7+
* * * * Copyright 2019-2022 the original author or authors.
8+
* * * *
9+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
10+
* * * * you may not use this file except in compliance with the License.
11+
* * * * You may obtain a copy of the License at
12+
* * * *
13+
* * * * https://www.apache.org/licenses/LICENSE-2.0
14+
* * * *
15+
* * * * Unless required by applicable law or agreed to in writing, software
16+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
17+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* * * * See the License for the specific language governing permissions and
19+
* * * * limitations under the License.
20+
* * *
21+
* *
22+
*
23+
*/
24+
import java.util.Iterator;
25+
26+
import com.fasterxml.jackson.databind.JavaType;
27+
import io.swagger.v3.core.converter.AnnotatedType;
28+
import io.swagger.v3.core.converter.ModelConverter;
29+
import io.swagger.v3.core.converter.ModelConverterContext;
30+
import io.swagger.v3.oas.models.media.Schema;
31+
import org.apache.commons.lang3.StringUtils;
32+
import org.springdoc.core.converters.models.Sort;
33+
import org.springdoc.core.providers.ObjectMapperProvider;
34+
35+
/**
36+
* The Spring Data Sort type model converter.
37+
* @author daniel-shuy
38+
*/
39+
public class SortOpenAPIConverter implements ModelConverter {
40+
41+
private static final String SORT_TO_REPLACE = "org.springframework.data.domain.Sort";
42+
43+
/**
44+
* The constant SORT.
45+
*/
46+
private static final AnnotatedType SORT = new AnnotatedType(Sort.class).resolveAsRef(true);
47+
48+
/**
49+
* The Spring doc object mapper.
50+
*/
51+
private final ObjectMapperProvider springDocObjectMapper;
52+
53+
/**
54+
* Instantiates a new Sort open api converter.
55+
*
56+
* @param springDocObjectMapper the spring doc object mapper
57+
*/
58+
public SortOpenAPIConverter(ObjectMapperProvider springDocObjectMapper) {
59+
this.springDocObjectMapper = springDocObjectMapper;
60+
}
61+
62+
/**
63+
* Resolve schema.
64+
*
65+
* @param type the type
66+
* @param context the context
67+
* @param chain the chain
68+
* @return the schema
69+
*/
70+
@Override
71+
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
72+
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
73+
if (javaType != null) {
74+
Class<?> cls = javaType.getRawClass();
75+
if (SORT_TO_REPLACE.equals(cls.getCanonicalName())) {
76+
if (!type.isSchemaProperty())
77+
type = SORT;
78+
else
79+
type.name(cls.getSimpleName() + StringUtils.capitalize(type.getParent().getType()));
80+
}
81+
}
82+
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
83+
}
84+
85+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.springdoc.core.converters.models;
2+
3+
/*
4+
*
5+
* *
6+
* * *
7+
* * * * Copyright 2019-2022 the original author or authors.
8+
* * * *
9+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
10+
* * * * you may not use this file except in compliance with the License.
11+
* * * * You may obtain a copy of the License at
12+
* * * *
13+
* * * * https://www.apache.org/licenses/LICENSE-2.0
14+
* * * *
15+
* * * * Unless required by applicable law or agreed to in writing, software
16+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
17+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* * * * See the License for the specific language governing permissions and
19+
* * * * limitations under the License.
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
import java.util.List;
26+
import java.util.Objects;
27+
28+
import io.swagger.v3.oas.annotations.Parameter;
29+
import io.swagger.v3.oas.annotations.media.ArraySchema;
30+
import io.swagger.v3.oas.annotations.media.Schema;
31+
32+
/**
33+
* The Sort type.
34+
* @author daniel-shuy
35+
*/
36+
public class Sort {
37+
38+
/**
39+
* The Sort.
40+
*/
41+
@Parameter(description = "Sorting criteria in the format: property,(asc|desc). "
42+
+ "Default sort order is ascending. " + "Multiple sort criteria are supported."
43+
, name = "sort"
44+
, array = @ArraySchema(schema = @Schema(type = "string")))
45+
private List<String> sort;
46+
47+
/**
48+
* Instantiates a new Sort.
49+
*
50+
* @param sort the sort
51+
*/
52+
public Sort(List<String> sort) {
53+
this.sort = sort;
54+
}
55+
56+
/**
57+
* Gets sort.
58+
*
59+
* @return the sort
60+
*/
61+
public List<String> getSort() {
62+
return sort;
63+
}
64+
65+
/**
66+
* Sets sort.
67+
*
68+
* @param sort the sort
69+
*/
70+
public void setSort(List<String> sort) {
71+
if (sort == null) {
72+
this.sort.clear();
73+
}
74+
else {
75+
this.sort = sort;
76+
}
77+
}
78+
79+
/**
80+
* Add sort.
81+
*
82+
* @param sort the sort
83+
*/
84+
public void addSort(String sort) {
85+
this.sort.add(sort);
86+
}
87+
88+
@Override
89+
public boolean equals(Object o) {
90+
if (this == o) return true;
91+
if (o == null || getClass() != o.getClass()) return false;
92+
Sort sort = (Sort) o;
93+
return Objects.equals(this.sort, sort.sort);
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return Objects.hash(sort);
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return "Sort{" +
104+
"sort=" + sort +
105+
'}';
106+
}
107+
}

0 commit comments

Comments
 (0)