Skip to content

Unable to set null example in model property with ModelPropertyBuilderPlugin #3899

@lhannest

Description

@lhannest

I'm using version 2.9.2

I want an Integer model property to have a null value in the Swagger UI page.

But, no matter what I do the model property shows up as 0 on the Swagger UI page.

I tried creating a ModelPropertyBuilderPlugin that finds model properties with the following annotation:

@ApiNullExample
public Long nullableField;

And sets the builder's example to null:

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1)
public class MyModelPropertyBuilderPlugin implements ModelPropertyBuilderPlugin {
    @Override
    public void apply(ModelPropertyContext context) {
        Optional<ApiNullExample> optional = findAnnotation(context, ApiNullExample.class);

        if (optional.isPresent()) {
            context.getBuilder().allowEmptyValue(true);
            context.getBuilder().example((Object) null);
        }
    }
    
    ... 

It does find the annotation, but then it's unable to set the example to null because the builder always takes the previous value (which another ModelPropertyBuilderPlugin that I have no control over has set to "") if the input is null.

Worse, even if I manually set the example to null in my debugger, it still shows up as 0 in the Swagger UI page.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions