Skip to content

Example value cannot be specified without having to specify a schema #352

Closed
@cnusp

Description

@cnusp

As far as possible I let springdoc-openapi autogenerate my OpenAPI specification, that is without my intervention. But examples need to be provided manually, because the autogenerated examples do not contain sensible values:

@RequestBody(
        content = @Content(
                examples = @ExampleObject(
                        value = "{ \"a\": \"sensible value\" }"
                )
        )
)
public void postMyRequestBody(@RequestBody MyRequestBody myRequestBody) {
    throw new NotImplementedException();
}

That compiles, but gives me a runtime error. Even though the type of the request body, defined as @RequestBody in the controller method, is actually known (and even used for the autogenerated example if I do not specify my own).

I need to add schema = @Schema(implementation = MyRequestBody.class) (which is redundant and unchecked, thus can lead to a discrepancy between springdoc-openapi annotation and the actual controller method). And even if I explicitly specify the schema, the example JSON is not validated (not parsed?) using that schema.

@RequestBody(
        content = @Content(
                schema = @Schema(implementation = MyRequestBody.class),
                examples = @ExampleObject(
                        value = "{ \"a\": 1 }"
                )
        )
)
public void postMyRequestBody(@RequestBody MyRequestBody myRequestBody) {
    throw new NotImplementedException();
}

Definition of done:

  • automatically use MyRequestBody as the schema class
  • validate the example (using that schema class)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions