oas在属性上忽略@schema(必需= false),如果标记为@notnull

发布于 2025-02-07 21:12:32 字数 941 浏览 1 评论 0原文

我的模型具有我不需要的字段的模型:

import io.swagger.v3.oas.annotations.media.Schema;

import javax.validation.constraints.NotNull;

public class Model {
    @NotNull
    @Schema(required = false)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

但是SpringDoc自动形式将其标记为需要(我知道这是@notnull注释的原因),

 "schemas": {
      "Model": {
        "required": [
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      },

因此我如何在生成的文档中不需要字段并留下两个注释,我的意思是@notnull和@schema?

使用的依赖性:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.7</version>
</dependency>

I have model with field i want to make not required:

import io.swagger.v3.oas.annotations.media.Schema;

import javax.validation.constraints.NotNull;

public class Model {
    @NotNull
    @Schema(required = false)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

but springdoc automaticaly marks it as required(i know that it's cause of @NotNull annotation)

 "schemas": {
      "Model": {
        "required": [
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      },

So how i can make field not required in generated documentation and leave both annotations i mean @NotNull and @Schema?

Used dependencies:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.7</version>
</dependency>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2025-02-14 21:12:32

您可以使用parametercustomizer覆盖@notnull这样的注释:

@Component
public class ParameterCustomizer implements org.springdoc.core.customizers.ParameterCustomizer {

    @Override
    public Parameter customize(Parameter parameterModel, java.lang.reflect.Parameter parameter, HandlerMethod handlerMethod) {
        NotNull annotation = parameter.getAnnotation(NotNull.class);
        if (annotation != null) {
            parameterModel.required(false);
        }
        return parameterModel;
    }
}

You can override the @NotNull annotation using ParameterCustomizer like this:

@Component
public class ParameterCustomizer implements org.springdoc.core.customizers.ParameterCustomizer {

    @Override
    public Parameter customize(Parameter parameterModel, java.lang.reflect.Parameter parameter, HandlerMethod handlerMethod) {
        NotNull annotation = parameter.getAnnotation(NotNull.class);
        if (annotation != null) {
            parameterModel.required(false);
        }
        return parameterModel;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文