28
28
29
29
30
30
import java .util .Iterator ;
31
+ import java .util .Optional ;
31
32
32
33
import com .fasterxml .jackson .databind .JavaType ;
33
34
import io .swagger .v3 .core .converter .ModelConverter ;
43
44
44
45
/**
45
46
* The type Hateoas links converter.
46
- *
47
+ *
47
48
* @author bnasslahsen
48
49
*/
49
50
public class HateoasLinksConverter implements ModelConverter {
@@ -70,19 +71,30 @@ public Schema<?> resolve(
70
71
) {
71
72
JavaType javaType = springDocObjectMapper .jsonMapper ().constructType (type .getType ());
72
73
if (javaType != null && RepresentationModel .class .isAssignableFrom (javaType .getRawClass ())) {
73
- Schema <?> schema = chain .next ().resolve (type , context , chain );
74
- String schemaName = schema .get$ref ().substring (Components .COMPONENTS_SCHEMAS_REF .length ());
75
- Schema original = context .getDefinedModels ().get (schemaName );
76
- Object links = original .getProperties ().get ("_links" );
77
- if (links instanceof JsonSchema jsonSchema ) {
78
- jsonSchema .set$ref (AnnotationsUtils .COMPONENTS_REF + "Links" );
79
- jsonSchema .setType (null );
80
- jsonSchema .setItems (null );
81
- jsonSchema .setTypes (null );
82
- } else if (links instanceof ArraySchema arraySchema ){
83
- arraySchema .set$ref (AnnotationsUtils .COMPONENTS_REF + "Links" );
74
+ Schema <?> schema = chain .next ().resolve (type , context , chain );
75
+ if (schema != null ) {
76
+ String schemaName = Optional .ofNullable (schema .get$ref ())
77
+ .filter (ref -> ref .startsWith (Components .COMPONENTS_SCHEMAS_REF ))
78
+ .map (ref -> ref .substring (Components .COMPONENTS_SCHEMAS_REF .length ()))
79
+ .orElse (schema .getName ());
80
+ if (schemaName != null ) {
81
+ Schema original = context .getDefinedModels ().get (schemaName );
82
+ if (original == null || original .getProperties () == null ) {
83
+ return schema ;
84
+ }
85
+ Object links = original .getProperties ().get ("_links" );
86
+ if (links instanceof JsonSchema jsonSchema ) {
87
+ jsonSchema .set$ref (AnnotationsUtils .COMPONENTS_REF + "Links" );
88
+ jsonSchema .setType (null );
89
+ jsonSchema .setItems (null );
90
+ jsonSchema .setTypes (null );
91
+ }
92
+ else if (links instanceof ArraySchema arraySchema ) {
93
+ arraySchema .set$ref (AnnotationsUtils .COMPONENTS_REF + "Links" );
94
+ }
84
95
}
85
- return schema ;
96
+ }
97
+ return schema ;
86
98
}
87
99
return chain .hasNext () ? chain .next ().resolve (type , context , chain ) : null ;
88
100
}
0 commit comments