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