Make Swagger uses primitive int and boolean in generated models

By default, for "type": "boolean" in the JSON specification, Swagger will generate a Boolean field (object, not primitive, null) in the model. Is there a way to get Swagger to generate Boolean fields (primitive, not null) in models?

Rationale: Spring MVC is going to initialize these fields with null for invalid input, which is undesirable. Better save them with default values.

Same question with int vs Integer .

NB: Swagger has its own concept of "primitive" types, which is completely unrelated to Java primitives and is not what I am looking for.

+5
source share
1 answer

It seems to be impossible.

I took a look at swagger-core and springfox (Swagger for Spring API). I just found the source code for converting / translating primitive types.

So, swagger-core I found the PrimitiveType enumeration. As you can see, there are no primitive Java types; all swagger types map to Java shells. Given that this enumeration is used internally and cannot be replaced (unless you make your own version), I assume that it is not possible to change the behavior of this type.

Same thing in springfox . I found a TypeNameExtractor that uses the fasterxml TypeResolver class to resolve types. This class, in turn, uses ResolvedPrimitiveType to get type mappings. As you can see, the type of resolution is also quite deep, so I believe that there is also no way to change it in any way.

I hope you find some useful insights.

+7
source

Source: https://habr.com/ru/post/1269601/


All Articles