Skip to content

Commit d6b6ba5

Browse files
Rework
1 parent 28aecd4 commit d6b6ba5

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ import scala.collection.JavaConverters._
2525
class OpenAPIScalaCustomizer(components: Components) extends OpenApiCustomizer {
2626

2727
override def customise(openAPIOutOfSync: OpenAPI): Unit = {
28-
// this is needed as for some reason springdoc-openapi cache the `OpenAPI` at the beginning
29-
// and newly added `Components` are not taken into account on JSON/YAML generation
30-
openAPIOutOfSync.setComponents(components)
28+
// Serialize the customized Components object to a JSON string.
29+
val jsonRepresentation = Json.pretty(components)
3130

32-
fixResponsesReturningUnit(openAPIOutOfSync)
33-
34-
val jsonRepresentation = Json.pretty(openAPIOutOfSync)
31+
// Deserialize the JSON string back into a new Components object to iron out any issues.
32+
val newComponents = Json.mapper().readValue(jsonRepresentation, classOf[Components])
3533

36-
// Deserialize the JSON string back into a new OpenAPI object
37-
val newOpenAPI = Json.mapper().readValue(jsonRepresentation, classOf[OpenAPI])
34+
// Finally replace the Components object in the OpenAPI instance.
35+
// This is needed as for some reason springdoc-openapi cache the `OpenAPI` at the beginning
36+
// and newly added `Components` are not taken into account on JSON/YAML generation.
37+
openAPIOutOfSync.setComponents(newComponents)
3838

39-
// Replace the root by copying properties from the new OpenAPI object
40-
openAPIOutOfSync.setComponents(newOpenAPI.getComponents)
39+
fixResponsesReturningUnit(openAPIOutOfSync)
4140
}
4241

4342
private def fixResponsesReturningUnit(openAPI: OpenAPI): Unit = {

0 commit comments

Comments
 (0)