vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

OpenAPIContract fails for JsonArray examples with ClassCastException

Open Jotschi opened this issue 2 years ago • 2 comments

Version

4.4.1

Context

When I construct my OpenAPIContract using io.vertx:vertx-web-openapi-router I get a ClassCastException.

Caused by: java.lang.ClassCastException: class io.vertx.core.json.JsonArray cannot be cast to class io.vertx.core.json.JsonObject (io.vertx.core.json.JsonArray and io.vertx.core.json.JsonObject are in unnamed module of loader 'app')
	at io.vertx.core.json.JsonObject.getJsonObject(JsonObject.java:566)
	at io.vertx.json.schema.impl.Ref.reduce(Ref.java:292)
	at io.vertx.json.schema.impl.Ref.resolveUri(Ref.java:284)
	at io.vertx.json.schema.impl.Ref.lambda$resolve$7(Ref.java:121)
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1220)
	at io.vertx.json.schema.impl.Ref.resolve(Ref.java:121)
	at io.vertx.json.schema.impl.Ref.resolve(Ref.java:46)
	at io.vertx.json.schema.impl.SchemaRepositoryImpl.resolve(SchemaRepositoryImpl.java:220)
	at io.vertx.openapi.contract.OpenAPIVersion.lambda$resolve$3(OpenAPIVersion.java:76)
	at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
	at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
	at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
	at io.vertx.core.impl.TaskQueue.run(TaskQueue.java:76)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:833)

I believe the error happens when resolving the example in my spec. The example lists an array but the implementation expects an JsonObject.

    ContentListResponse:
      type: array
      items:
        $ref: "#/components/schemas/ContentResponse"
      example:
        - uuid: "4fbe29c9-c69a-4572-85e4-19bc433c6cf7"
          name: "test"
        - uuid: "054e4e69-21b2-4662-aacd-2c7ee2d3dff2"
          name: "test2"

Do you have a reproducer?

https://github.com/Jotschi/openapi-router-example-reproducer

Steps to reproduce

  1. Run ContractTest

Jotschi avatar Apr 18 '23 16:04 Jotschi

Hi @Jotschi ,

I wanted to debug your example, but this is really hard with a contract which is that big. Could you create a Contract which is as small as possible?

pk-work avatar Jun 15 '23 05:06 pk-work

@pk-work I have reduced the contract as much as possible.

Jotschi avatar Jun 15 '23 07:06 Jotschi

Hi,

the OpenAPI spec is wrong. examples must be a map [1], and not an array.

[1] https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#media-type-object

I hope this solves your issue.

pk-work avatar May 03 '24 20:05 pk-work