24
24
import io .swagger .v3 .core .converter .ModelConverters ;
25
25
import io .swagger .v3 .core .converter .ResolvedSchema ;
26
26
import io .swagger .v3 .core .util .Json ;
27
- import io .swagger .v3 .oas .annotations .media .Schema ;
28
27
import io .swagger .v3 .oas .models .media .MapSchema ;
29
28
import io .swagger .v3 .oas .models .media .ObjectSchema ;
30
29
import io .swagger .v3 .oas .models .media .StringSchema ;
31
30
import org .springdoc .core .converters .Pageable ;
32
31
import org .springdoc .core .customizers .OpenApiCustomiser ;
32
+ import org .springdoc .core .hal .RepresentationModelLinksOASMixin ;
33
33
34
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
34
35
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
35
36
import org .springframework .context .annotation .Bean ;
36
37
import org .springframework .context .annotation .Configuration ;
37
38
import org .springframework .data .rest .core .config .RepositoryRestConfiguration ;
38
39
import org .springframework .hateoas .Link ;
39
40
import org .springframework .hateoas .Links ;
40
41
import org .springframework .hateoas .RepresentationModel ;
41
- import org .springframework .hateoas .mediatype .hal .RepresentationModelMixin ;
42
42
43
43
import static org .springdoc .core .Constants .SPRINGDOC_ENABLED ;
44
44
import static org .springdoc .core .SpringDocUtils .getConfig ;
@@ -52,35 +52,36 @@ public class SpringDocDataRestConfiguration {
52
52
.replaceWithClass (org .springframework .data .domain .PageRequest .class ,Pageable .class );
53
53
}
54
54
55
- @ Bean
56
- public HalProvider halProvider (RepositoryRestConfiguration repositoryRestConfiguration ) {
57
- return new HalProvider (repositoryRestConfiguration );
58
- }
55
+ @ Configuration
56
+ @ ConditionalOnClass (RepositoryRestConfiguration .class )
57
+ class HalProviderConfiguration {
59
58
60
- /**
61
- * Registers an OpenApiCustomiser and a jackson mixin to ensure the definition of `Links` matches the serialized
62
- * output. This is done because the customer serializer converts the data to a map before serializing it.
63
- *
64
- * @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)
65
- */
66
- @ Bean
67
- public OpenApiCustomiser linksSchemaCustomiser (RepositoryRestConfiguration repositoryRestConfiguration ) {
68
- if (!repositoryRestConfiguration .useHalAsDefaultJsonMediaType ()) {
69
- return openApi -> {};
70
- }
71
- Json .mapper ().addMixIn (RepresentationModel .class , RepresentationModelLinksOASMixin .class );
72
- ResolvedSchema resolvedLinkSchema = ModelConverters .getInstance ()
73
- .resolveAsResolvedSchema (new AnnotatedType (Link .class ));
74
- return openApi -> openApi
75
- .schema ("Link" , resolvedLinkSchema .schema )
76
- .schema ("Links" , new MapSchema ()
77
- .additionalProperties (new StringSchema ())
78
- .additionalProperties (new ObjectSchema ().$ref ("#/components/schemas/Link" )));
79
- }
59
+ @ Bean
60
+ public HalProvider halProvider (RepositoryRestConfiguration repositoryRestConfiguration ) {
61
+ return new HalProvider (repositoryRestConfiguration );
62
+ }
80
63
81
- abstract static class RepresentationModelLinksOASMixin extends RepresentationModelMixin {
82
- @ Override
83
- @ Schema (ref = "#/components/schemas/Links" )
84
- public abstract Links getLinks ();
85
- }
64
+ /**
65
+ * Registers an OpenApiCustomiser and a jackson mixin to ensure the definition of `Links` matches the serialized
66
+ * output. This is done because the customer serializer converts the data to a map before serializing it.
67
+ *
68
+ * @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)
69
+ */
70
+ @ Bean
71
+ public OpenApiCustomiser linksSchemaCustomiser (RepositoryRestConfiguration repositoryRestConfiguration ) {
72
+ if (!repositoryRestConfiguration .useHalAsDefaultJsonMediaType ()) {
73
+ return openApi -> {};
74
+ }
75
+ Json .mapper ().addMixIn (RepresentationModel .class , RepresentationModelLinksOASMixin .class );
76
+
77
+ ResolvedSchema resolvedLinkSchema = ModelConverters .getInstance ()
78
+ .resolveAsResolvedSchema (new AnnotatedType (Link .class ));
79
+
80
+ return openApi -> openApi
81
+ .schema ("Link" , resolvedLinkSchema .schema )
82
+ .schema ("Links" , new MapSchema ()
83
+ .additionalProperties (new StringSchema ())
84
+ .additionalProperties (new ObjectSchema ().$ref ("#/components/schemas/Link" )));
85
+ }
86
+ }
86
87
}
0 commit comments