Skip to content

Commit a9e5246

Browse files
Feature/33 fix npe (#36)
* wip * Fix NPE * Fix wording
1 parent 4fdfc42 commit a9e5246

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

library/src/main/scala/za/co/absa/springdocopenapiscala/OpenAPIScalaCustomizer.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ class OpenAPIScalaCustomizer(components: Components) extends OpenApiCustomizer {
3838
allOperations.foreach { case (_, operation) =>
3939
val responses = operation.getResponses.asScala
4040
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+
})
4549
}
4650
}
4751
}
4852
}
49-
5053
}

library/src/test/scala/za/co/absa/springdocopenapiscala/OpenAPIScalaCustomizerSpec.scala

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,72 @@ class OpenAPIScalaCustomizerSpec extends AnyFlatSpec {
8080
)
8181
}
8282

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+
83151
}

0 commit comments

Comments
 (0)