javalin / javalin-openapi

Annotation processor for compile-time OpenAPI & JsonSchema, with out-of-the-box support for Javalin 5.x, Swagger & ReDoc
https://github.com/javalin/javalin-openapi/wiki
Apache License 2.0
45 stars 17 forks source link

`OpenApiExample` doesn't support lists #193

Closed dennisameling closed 1 year ago

dennisameling commented 1 year ago

Describe the feature

Currently, the @OpenApiExample annotation in this library only supports strings. Let's consider a scenario where we want to put a list of UUIDs in the example:

@get:OpenApiExample("[\"f2be25d0-074c-4f2e-b60d-4a3af8899c60\"]")
val userIds: List<UUID>,

In the spec, this leads to the following incorrect behavior:

      "properties" : {
          "userIds" : {
            "type" : "array",
            "items" : {
              "type" : "string"
            },
            "example" : "[\"e859c532-7852-4d6e-9084-305aa78b496b\"]"
          }
        },

Would it be possible to support lists as well? Thank you!

Additional context

Array examples are supported in the OpenAPI standard: https://swagger.io/docs/specification/adding-examples/

dzikoysk commented 1 year ago

You can use a new annotation called @OpenApiExampleProperty to define more complex examples. For instance, this setup:

@OpenApiExample(objects = {
    @OpenApiExampleProperty(value = "2022-08-14T21:13:03.546Z"),
    @OpenApiExampleProperty(value = "2022-08-14T21:13:03.546Z")
})
public @NotNull String[] getTimestamps() {
    return new String[] { timestamp };
}

@OpenApiExample(objects = {
        @OpenApiExampleProperty(name = "Barbie", objects = {
                @OpenApiExampleProperty(name = "name", value = "Margot Robbie"),
                @OpenApiExampleProperty(name = "link", value = "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
        }),
})
public @NotNull Object[] getExampleObjects() {
    return new String[] { timestamp };
}

should be generated as:

"timestamps" : {
  "type" : "array",
  "items" : {
    "type" : "string"
  },
  "example" : [ "2022-08-14T21:13:03.546Z", "2022-08-14T21:13:03.546Z" ]
},
"exampleObjects" : {
  "type" : "array",
  "items" : {
    "type" : "object"
  },
  "example" : {
    "Barbie" : {
      "name" : "Margot Robbie",
      "link" : "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
    }
  }
},
dennisameling commented 1 year ago

Thank you! I can confirm everything works as expected with this new setup 🎉