1
+ /*
2
+ *
3
+ * *
4
+ * * *
5
+ * * * * Copyright 2025 the original author or authors.
6
+ * * * *
7
+ * * * * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * * * * you may not use this file except in compliance with the License.
9
+ * * * * You may obtain a copy of the License at
10
+ * * * *
11
+ * * * * https://www.apache.org/licenses/LICENSE-2.0
12
+ * * * *
13
+ * * * * Unless required by applicable law or agreed to in writing, software
14
+ * * * * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * * * * See the License for the specific language governing permissions and
17
+ * * * * limitations under the License.
18
+ * * *
19
+ * *
20
+ *
21
+ */
22
+
23
+ package org .springdoc .core .configuration ;
24
+
25
+ import com .fasterxml .jackson .databind .introspect .Annotated ;
26
+ import com .fasterxml .jackson .databind .jsontype .NamedType ;
27
+ import com .fasterxml .jackson .databind .module .SimpleModule ;
28
+ import io .swagger .v3 .core .jackson .SwaggerAnnotationIntrospector ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Arrays ;
31
+ import java .util .List ;
32
+
33
+ /**
34
+ * The type Spring doc sealed class module.
35
+ *
36
+ * @author sahil-ramagiri
37
+ */
38
+ public class SpringDocSealedClassModule extends SimpleModule {
39
+
40
+ @ Override
41
+ public void setupModule (SetupContext context ) {
42
+ context .insertAnnotationIntrospector (new RespectSealedClassAnnotationIntrospector ());
43
+ }
44
+
45
+ /**
46
+ * The type sealed class annotation introspector.
47
+ */
48
+ private static class RespectSealedClassAnnotationIntrospector extends SwaggerAnnotationIntrospector {
49
+
50
+ @ Override
51
+ public List <NamedType > findSubtypes (Annotated annotated ) {
52
+ ArrayList <NamedType > subTypes = new ArrayList <>();
53
+
54
+ if (annotated .getAnnotated () instanceof Class <?> clazz && clazz .isSealed ()) {
55
+ Class <?>[] permittedSubClasses = clazz .getPermittedSubclasses ();
56
+ if (permittedSubClasses .length > 0 ) {
57
+ Arrays .stream (permittedSubClasses ).map (NamedType ::new ).forEach (subTypes ::add );
58
+ }
59
+ }
60
+
61
+ return subTypes ;
62
+ }
63
+ }
64
+ }
0 commit comments