Skip to content

Commit ea9551c

Browse files
committed
Add ExtraTypesHandler to examples
1 parent fd1faae commit ea9551c

File tree

8 files changed

+124
-86
lines changed

8 files changed

+124
-86
lines changed

README.md

Lines changed: 89 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -165,90 +165,95 @@ class ExampleController @Autowired()(openAPIModelRegistration: OpenAPIModelRegis
165165
Can be found in this repo: [link](examples/springdoc-openapi-scala-1/simple). It generates the following OpenAPI JSON doc:
166166
```json
167167
{
168-
"openapi": "3.0.1",
169-
"info": {
170-
"title": "Example API with springdoc-openapi v1.x",
171-
"version": "1.0.0"
172-
},
173-
"servers": [
174-
{
175-
"url": "http://localhost:8080",
176-
"description": "Generated server url"
177-
}
178-
],
179-
"paths": {
180-
"/api/v1/example/some-endpoint": {
181-
"post": {
182-
"tags": [
183-
"example-controller"
184-
],
185-
"operationId": "someEndpoint",
186-
"requestBody": {
187-
"content": {
188-
"application/json": {
189-
"schema": {
190-
"$ref": "#/components/schemas/ExampleModelRequest"
191-
}
192-
}
193-
},
194-
"required": true
195-
},
196-
"responses": {
197-
"200": {
198-
"description": "OK",
199-
"content": {
200-
"application/json": {
201-
"schema": {
202-
"$ref": "#/components/schemas/ExampleModelResponse"
203-
}
204-
}
205-
}
206-
}
168+
"openapi": "3.0.1",
169+
"info": {
170+
"title": "Example API with springdoc-openapi v1.x",
171+
"version": "1.0.0"
172+
},
173+
"servers": [
174+
{
175+
"url": "http://localhost:8080",
176+
"description": "Generated server url"
177+
}
178+
],
179+
"paths": {
180+
"/api/v1/example/some-endpoint": {
181+
"post": {
182+
"tags": [
183+
"example-controller"
184+
],
185+
"operationId": "someEndpoint",
186+
"requestBody": {
187+
"content": {
188+
"application/json": {
189+
"schema": {
190+
"$ref": "#/components/schemas/ExampleModelRequest"
191+
}
192+
}
193+
},
194+
"required": true
195+
},
196+
"responses": {
197+
"200": {
198+
"description": "OK",
199+
"content": {
200+
"application/json": {
201+
"schema": {
202+
"$ref": "#/components/schemas/ExampleModelResponse"
207203
}
204+
}
208205
}
206+
}
209207
}
210-
},
211-
"components": {
212-
"schemas": {
213-
"ExampleModelRequest": {
214-
"required": [
215-
"a",
216-
"b"
217-
],
218-
"properties": {
219-
"a": {
220-
"type": "integer",
221-
"format": "int32"
222-
},
223-
"b": {
224-
"type": "string"
225-
},
226-
"c": {
227-
"type": "integer",
228-
"format": "int32"
229-
}
230-
}
231-
},
232-
"ExampleModelResponse": {
233-
"required": [
234-
"d",
235-
"e"
236-
],
237-
"properties": {
238-
"d": {
239-
"type": "array",
240-
"items": {
241-
"type": "integer",
242-
"format": "int32"
243-
}
244-
},
245-
"e": {
246-
"type": "boolean"
247-
}
248-
}
208+
}
209+
}
210+
},
211+
"components": {
212+
"schemas": {
213+
"ExampleModelRequest": {
214+
"required": [
215+
"a",
216+
"b",
217+
"d"
218+
],
219+
"properties": {
220+
"a": {
221+
"type": "integer",
222+
"format": "int32"
223+
},
224+
"b": {
225+
"type": "string"
226+
},
227+
"c": {
228+
"type": "integer",
229+
"format": "int32"
230+
},
231+
"d": {
232+
"type": "string",
233+
"format": "json"
234+
}
235+
}
236+
},
237+
"ExampleModelResponse": {
238+
"required": [
239+
"d",
240+
"e"
241+
],
242+
"properties": {
243+
"d": {
244+
"type": "array",
245+
"items": {
246+
"type": "integer",
247+
"format": "int32"
249248
}
249+
},
250+
"e": {
251+
"type": "boolean"
252+
}
250253
}
254+
}
251255
}
256+
}
252257
}
253258
```
254259

@@ -318,7 +323,8 @@ Can be found in this repo: [link](examples/springdoc-openapi-scala-2/simple). It
318323
"required": [
319324
"a",
320325
"b",
321-
"d"
326+
"d",
327+
"e"
322328
],
323329
"properties": {
324330
"a": {
@@ -339,6 +345,10 @@ Can be found in this repo: [link](examples/springdoc-openapi-scala-2/simple). It
339345
"OptionB",
340346
"OptionA"
341347
]
348+
},
349+
"e": {
350+
"type": "string",
351+
"format": "json"
342352
}
343353
}
344354
},

examples/springdoc-openapi-scala-1/simple/build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ lazy val root = (project in file("."))
2323
libraryDependencies ++= Seq(
2424
"za.co.absa" %% "springdoc-openapi-scala-1" % `springdoc-openapi-scala-1-version`,
2525
"org.springdoc" % "springdoc-openapi-webmvc-core" % "1.7.0",
26-
"org.springframework.boot" % "spring-boot-starter-web" % "2.6.6"
26+
"org.springframework.boot" % "spring-boot-starter-web" % "2.6.6",
27+
"com.fasterxml.jackson.core" % "jackson-databind" % "2.16.1"
2728
),
2829
webappWebInfClasses := true,
2930
inheritJarManifest := true

examples/springdoc-openapi-scala-1/simple/src/main/scala/za/co/absa/springdocopenapiscala/examples/simple/OpenAPIConfiguration.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package za.co.absa.springdocopenapiscala.examples.simple
1818

19+
import com.fasterxml.jackson.databind.JsonNode
1920
import io.swagger.v3.oas.models.OpenAPI
2021
import io.swagger.v3.oas.models.info.Info
22+
import io.swagger.v3.oas.models.media.Schema
2123
import org.springdoc.core.customizers.OpenApiCustomiser
2224
import org.springframework.context.annotation.{Bean, Configuration}
23-
2425
import za.co.absa.springdocopenapiscala.{Bundle, OpenAPIModelRegistration}
2526

27+
import scala.reflect.runtime.universe.typeOf
28+
2629
@Configuration
2730
class OpenAPIConfiguration {
2831

@@ -33,7 +36,14 @@ class OpenAPIConfiguration {
3336
.title("Example API with springdoc-openapi v1.x")
3437
.version("1.0.0")
3538
)
36-
)
39+
),
40+
OpenAPIModelRegistration.ExtraTypesHandling.simpleMapping {
41+
case t if t =:= typeOf[JsonNode] =>
42+
val schema = new Schema
43+
schema.setType("string")
44+
schema.setFormat("json")
45+
schema
46+
}
3747
)
3848

3949
@Bean

examples/springdoc-openapi-scala-1/simple/src/main/scala/za/co/absa/springdocopenapiscala/examples/simple/model/ExampleModelRequest.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616

1717
package za.co.absa.springdocopenapiscala.examples.simple.model
1818

19-
case class ExampleModelRequest(a: Int, b: String, c: Option[Int])
19+
import com.fasterxml.jackson.databind.JsonNode
20+
21+
case class ExampleModelRequest(a: Int, b: String, c: Option[Int], d: JsonNode)

examples/springdoc-openapi-scala-2/simple/build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ lazy val root = (project in file("."))
2323
libraryDependencies ++= Seq(
2424
"za.co.absa" %% "springdoc-openapi-scala-2" % `springdoc-openapi-scala-2-version`,
2525
"org.springdoc" % "springdoc-openapi-starter-webmvc-api" % "2.3.0",
26-
"org.springframework.boot" % "spring-boot-starter-web" % "3.2.0"
26+
"org.springframework.boot" % "spring-boot-starter-web" % "3.2.0",
27+
"org.playframework" %% "play-json" % "3.0.1"
2728
),
2829
webappWebInfClasses := true,
2930
inheritJarManifest := true

examples/springdoc-openapi-scala-2/simple/src/main/scala/za/co/absa/springdocopenapiscala/examples/simple/OpenAPIConfiguration.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package za.co.absa.springdocopenapiscala.examples.simple
1818

1919
import io.swagger.v3.oas.models.OpenAPI
2020
import io.swagger.v3.oas.models.info.Info
21+
import io.swagger.v3.oas.models.media.Schema
2122
import org.springdoc.core.customizers.OpenApiCustomizer
2223
import org.springframework.context.annotation.{Bean, Configuration}
24+
import play.api.libs.json.JsValue
25+
26+
import scala.reflect.runtime.universe.typeOf
2327

2428
import za.co.absa.springdocopenapiscala.{Bundle, OpenAPIModelRegistration}
2529

@@ -33,7 +37,14 @@ class OpenAPIConfiguration {
3337
.title("Example API with springdoc-openapi v2.x")
3438
.version("1.0.0")
3539
)
36-
)
40+
),
41+
OpenAPIModelRegistration.ExtraTypesHandling.simpleMapping {
42+
case t if t =:= typeOf[JsValue] =>
43+
val schema = new Schema
44+
schema.setType("string")
45+
schema.setFormat("json")
46+
schema
47+
}
3748
)
3849

3950
@Bean

examples/springdoc-openapi-scala-2/simple/src/main/scala/za/co/absa/springdocopenapiscala/examples/simple/model/ExampleModelRequest.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package za.co.absa.springdocopenapiscala.examples.simple.model
1818

19+
import play.api.libs.json.JsValue
20+
1921
object SimpleEnums extends Enumeration {
2022

2123
type SimpleEnum = Value
@@ -24,4 +26,4 @@ object SimpleEnums extends Enumeration {
2426

2527
}
2628

27-
case class ExampleModelRequest(a: Int, b: String, c: Option[Int], d: SimpleEnums.SimpleEnum)
29+
case class ExampleModelRequest(a: Int, b: String, c: Option[Int], d: SimpleEnums.SimpleEnum, e: JsValue)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ object OpenAPIModelRegistration {
230230
* val schema = new Schema
231231
* schema.setType("string")
232232
* schema.setFormat("json")
233+
* schema
233234
* }
234235
* }}}
235236
*/

0 commit comments

Comments
 (0)