|
18 | 18 |
|
19 | 19 | package org.springdoc.data.rest;
|
20 | 20 |
|
21 |
| -import java.util.Optional; |
22 |
| - |
23 | 21 | import com.fasterxml.jackson.core.JsonGenerator;
|
24 | 22 | import com.fasterxml.jackson.databind.SerializerProvider;
|
25 | 23 | import com.querydsl.core.types.Predicate;
|
|
31 | 29 | import io.swagger.v3.oas.models.media.ObjectSchema;
|
32 | 30 | import io.swagger.v3.oas.models.media.StringSchema;
|
33 | 31 | import org.springdoc.core.customizers.OpenApiCustomiser;
|
| 32 | +import org.springdoc.data.rest.converters.CollectionModelContentConverter; |
34 | 33 | import org.springdoc.data.rest.converters.Pageable;
|
35 | 34 | import org.springdoc.data.rest.converters.RepresentationModelLinksOASMixin;
|
36 | 35 | import org.springdoc.data.rest.customisers.QuerydslPredicateOperationCustomizer;
|
37 |
| - |
38 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
39 | 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
40 | 38 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
41 | 39 | import org.springframework.context.annotation.Bean;
|
| 40 | +import org.springframework.context.annotation.ComponentScan; |
42 | 41 | import org.springframework.context.annotation.Configuration;
|
43 |
| -import org.springframework.context.annotation.Lazy; |
44 | 42 | import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
|
45 | 43 | import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
46 | 44 | import org.springframework.hateoas.Link;
|
47 | 45 | import org.springframework.hateoas.Links;
|
48 | 46 | import org.springframework.hateoas.RepresentationModel;
|
| 47 | +import org.springframework.hateoas.server.LinkRelationProvider; |
| 48 | + |
| 49 | +import java.util.Optional; |
49 | 50 |
|
50 | 51 | import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
|
51 | 52 | import static org.springdoc.core.SpringDocUtils.getConfig;
|
52 | 53 |
|
53 | 54 | @Configuration
|
54 | 55 | @ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
|
| 56 | +@ComponentScan |
55 | 57 | public class SpringDocDataRestConfiguration {
|
56 | 58 |
|
57 |
| - static { |
58 |
| - getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, Pageable.class) |
59 |
| - .replaceWithClass(org.springframework.data.domain.PageRequest.class, Pageable.class); |
60 |
| - } |
61 |
| - |
62 |
| - @ConditionalOnClass(value = { QuerydslBindingsFactory.class }) |
63 |
| - class QuerydslProvider { |
| 59 | + static { |
| 60 | + getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, Pageable.class) |
| 61 | + .replaceWithClass(org.springframework.data.domain.PageRequest.class, Pageable.class); |
| 62 | + } |
64 | 63 |
|
65 |
| - @Bean |
66 |
| - @ConditionalOnMissingBean |
67 |
| - @Lazy(false) |
68 |
| - QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomizer(Optional<QuerydslBindingsFactory> querydslBindingsFactory) { |
69 |
| - if (querydslBindingsFactory.isPresent()) { |
70 |
| - getConfig().addRequestWrapperToIgnore(Predicate.class); |
71 |
| - return new QuerydslPredicateOperationCustomizer(querydslBindingsFactory.get()); |
72 |
| - } |
73 |
| - return null; |
74 |
| - } |
75 |
| - } |
| 64 | + @ConditionalOnClass(value = {QuerydslBindingsFactory.class}) |
| 65 | + class QuerydslProvider { |
76 | 66 |
|
| 67 | + @Bean |
| 68 | + @ConditionalOnMissingBean |
| 69 | + QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomizer(Optional<QuerydslBindingsFactory> querydslBindingsFactory) { |
| 70 | + if (querydslBindingsFactory.isPresent()) { |
| 71 | + getConfig().addRequestWrapperToIgnore(Predicate.class); |
| 72 | + return new QuerydslPredicateOperationCustomizer(querydslBindingsFactory.get()); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
77 | 77 |
|
78 |
| - @Bean |
79 |
| - @ConditionalOnMissingBean |
80 |
| - @Lazy(false) |
81 |
| - HalProvider halProvider(Optional<RepositoryRestConfiguration> repositoryRestConfiguration) { |
82 |
| - return new HalProvider(repositoryRestConfiguration); |
83 |
| - } |
| 78 | + @Bean |
| 79 | + CollectionModelContentConverter collectionModelContentConverter(HalProvider halProvider, LinkRelationProvider linkRelationProvider) { |
| 80 | + return halProvider.isEnabled() ? new CollectionModelContentConverter(linkRelationProvider) : null; |
| 81 | + } |
84 | 82 |
|
85 |
| - /** |
86 |
| - * Registers an OpenApiCustomiser and a jackson mixin to ensure the definition of `Links` matches the serialized |
87 |
| - * output. This is done because the customer serializer converts the data to a map before serializing it. |
88 |
| - * |
89 |
| - * @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider) |
90 |
| - */ |
91 |
| - @Bean |
92 |
| - @Lazy(false) |
93 |
| - OpenApiCustomiser linksSchemaCustomiser(Optional<RepositoryRestConfiguration> repositoryRestConfiguration) { |
94 |
| - if (!repositoryRestConfiguration.isPresent() || !repositoryRestConfiguration.get().useHalAsDefaultJsonMediaType()) { |
95 |
| - return openApi -> { |
96 |
| - }; |
97 |
| - } |
98 |
| - Json.mapper().addMixIn(RepresentationModel.class, RepresentationModelLinksOASMixin.class); |
| 83 | + /** |
| 84 | + * Registers an OpenApiCustomiser and a jackson mixin to ensure the definition of `Links` matches the serialized |
| 85 | + * output. This is done because the customer serializer converts the data to a map before serializing it. |
| 86 | + * |
| 87 | + * @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider) |
| 88 | + */ |
| 89 | + @Bean |
| 90 | + OpenApiCustomiser linksSchemaCustomiser(HalProvider halProvider) { |
| 91 | + if (!halProvider.isEnabled()) { |
| 92 | + return openApi -> { |
| 93 | + }; |
| 94 | + } |
| 95 | + Json.mapper().addMixIn(RepresentationModel.class, RepresentationModelLinksOASMixin.class); |
99 | 96 |
|
100 |
| - ResolvedSchema resolvedLinkSchema = ModelConverters.getInstance() |
101 |
| - .resolveAsResolvedSchema(new AnnotatedType(Link.class).resolveAsRef(true)); |
| 97 | + ResolvedSchema resolvedLinkSchema = ModelConverters.getInstance() |
| 98 | + .resolveAsResolvedSchema(new AnnotatedType(Link.class).resolveAsRef(true)); |
102 | 99 |
|
103 |
| - return openApi -> openApi |
104 |
| - .schema("Link", resolvedLinkSchema.schema) |
105 |
| - .schema("Links", new MapSchema() |
106 |
| - .additionalProperties(new StringSchema()) |
107 |
| - .additionalProperties(new ObjectSchema().$ref("#/components/schemas/Link"))); |
108 |
| - } |
| 100 | + return openApi -> openApi |
| 101 | + .schema("Link", resolvedLinkSchema.schema) |
| 102 | + .schema("Links", new MapSchema() |
| 103 | + .additionalProperties(new StringSchema()) |
| 104 | + .additionalProperties(new ObjectSchema().$ref("#/components/schemas/Link"))); |
| 105 | + } |
109 | 106 | }
|
0 commit comments