【问题标题】:How to handle OptionalInt parameters as integers in Swagger?如何在 Swagger 中将 OptionalInt 参数作为整数处理?
【发布时间】:2020-12-02 09:23:34
【问题描述】:

我正在尝试为现有 Jersey 项目获取 OpenAPI 定义。

我有很多方法采用OptionalInt 参数,例如:

public Response listAgents(@Auth AuthenticatedUser auth,
            @QueryParam("count") OptionalInt count, @QueryParam("start") OptionalInt start)

Swagger 将这些参数检测为对象(这是错误的):

      parameters:
      - name: count
        in: query
        schema:
          type: object
          properties:
            empty:
              type: boolean
            present:
              type: boolean
            asInt:
              type: integer
              format: int32

有没有办法将 Swagger 配置为将 OptionalInt 类型处理为整数类型,而无需在每个参数上使用 @Parameter(schema = @Schema(implementation = Integer.class)) 注释重写每个方法?

【问题讨论】:

    标签: java swagger


    【解决方案1】:

    我找到了解决办法:

    在配置文件中我放了一个模型转换器

    modelConverterClasses: 
    - fr.data.openapi.WaPropertyConverter
    

    我在其中定义了用于处理 OptionalInt 参数的架构:

    import io.swagger.v3.core.converter.ModelConverter;
    
    public class WaPropertyConverter implements ModelConverter {
        @Override
        public Schema<?> resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
            if (type.getType() instanceof SimpleType) {
                SimpleType type_ = (SimpleType) type.getType();
                if (type_.isTypeOrSubTypeOf(OptionalInt.class))
                    return optionalIntSchema();
    
            }
            if (chain.hasNext()) {
                return chain.next().resolve(type, context, chain);
            } else {
                return null;
            }
        }
    
        protected Schema<?> optionalIntSchema() {
            Schema<Integer> s = new Schema<Integer>();
            s.setType("integer");
            s.format("int32");
            return s;
        }
    }
    

    有了这个,我得到了所有 OptionalInt 的正确架构

          - name: count
            in: query
            schema:
              type: integer
              format: int32
    
    

    【讨论】:

      猜你喜欢
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2011-07-16
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多