File tree Expand file tree Collapse file tree 2 files changed +76
-5
lines changed
main/scala/za/co/absa/springdocopenapiscala
test/scala/za/co/absa/springdocopenapiscala Expand file tree Collapse file tree 2 files changed +76
-5
lines changed Original file line number Diff line number Diff line change @@ -38,13 +38,16 @@ class OpenAPIScalaCustomizer(components: Components) extends OpenApiCustomizer {
38
38
allOperations.foreach { case (_, operation) =>
39
39
val responses = operation.getResponses.asScala
40
40
responses.foreach { case (_, response) =>
41
- val isReturningUnit = response.getContent.asScala.exists(
42
- _._2.getSchema.get$ref == " #/components/schemas/BoxedUnit"
43
- )
44
- if (isReturningUnit) response.setContent(None .orNull)
41
+ val contentOpt = Option (response.getContent)
42
+ contentOpt.map(_.asScala.foreach { case (_, mediaType) =>
43
+ val schemaOpt = Option (mediaType.getSchema)
44
+ schemaOpt.map { schema =>
45
+ val isReturningUnit = schema.get$ref == " #/components/schemas/BoxedUnit"
46
+ if (isReturningUnit) response.setContent(None .orNull)
47
+ }
48
+ })
45
49
}
46
50
}
47
51
}
48
52
}
49
-
50
53
}
Original file line number Diff line number Diff line change @@ -80,4 +80,72 @@ class OpenAPIScalaCustomizerSpec extends AnyFlatSpec {
80
80
)
81
81
}
82
82
83
+ it should " do nothing if a response doesn't have content" in {
84
+ val components = new Components ()
85
+ val openAPIScalaCustomizer = new OpenAPIScalaCustomizer (components)
86
+
87
+ val openAPI = new OpenAPI ()
88
+ .paths(
89
+ new Paths ()
90
+ .addPathItem(
91
+ " /api/endpoint" ,
92
+ new PathItem ()
93
+ .delete(
94
+ new Operation ()
95
+ .responses(
96
+ new ApiResponses ()
97
+ .addApiResponse(
98
+ " 204" ,
99
+ new ApiResponse ()
100
+ .description(" No Content" )
101
+ )
102
+ )
103
+ )
104
+ )
105
+ )
106
+
107
+ openAPIScalaCustomizer.customise(openAPI)
108
+
109
+ assert(
110
+ Option (openAPI.getPaths.get(" /api/endpoint" ).getDelete.getResponses.get(" 204" ).getContent).isEmpty
111
+ )
112
+ }
113
+
114
+ it should " do nothing if a response doesn't have a schema" in {
115
+ val components = new Components ()
116
+ val openAPIScalaCustomizer = new OpenAPIScalaCustomizer (components)
117
+
118
+ val openAPI = new OpenAPI ()
119
+ .paths(
120
+ new Paths ()
121
+ .addPathItem(
122
+ " /api/endpoint" ,
123
+ new PathItem ()
124
+ .delete(
125
+ new Operation ()
126
+ .responses(
127
+ new ApiResponses ()
128
+ .addApiResponse(
129
+ " 204" ,
130
+ new ApiResponse ()
131
+ .content(
132
+ new Content ()
133
+ .addMediaType(
134
+ " */*" ,
135
+ new MediaType ()
136
+ )
137
+ )
138
+ )
139
+ )
140
+ )
141
+ )
142
+ )
143
+
144
+ openAPIScalaCustomizer.customise(openAPI)
145
+
146
+ assert(
147
+ Option (openAPI.getPaths.get(" /api/endpoint" ).getDelete.getResponses.get(" 204" ).getContent).isDefined
148
+ )
149
+ }
150
+
83
151
}
You can’t perform that action at this time.
0 commit comments