Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Swagger use primitive int and boolean in generated models

By default, for "type": "boolean" in JSON spec, Swagger will generate a Boolean (object, non-primitive, nullable) field in model. Is there a way to make Swagger generate boolean (primitive, non-nullable) fields in models instead?

The rationale is: Spring MVC is going to initialize these fields with null on invalid input, which is soooo undesirable. Better keep them with default values.

Same question with int vs Integer.

NB: Swagger has its own concept of "primitive" types which is totally unrelated to Java primitives and isn't what I seek.

like image 625
alamar Avatar asked Jul 07 '17 12:07

alamar


1 Answers

Seems like it is impossible.

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

So in swagger-core I found PrimitiveType enum. As you can see there is no primitive Java types mentioned, all the swagger types are mapped to Java wrappers. Taking into account that this enum is used internally and cannot be replaced (unless you make your own version) I assume it is impossible to change this type resolving behaviour.

Same in springfox. I found TypeNameExtractor that uses fasterxml's TypeResolver class for resolving types. This class in its turn uses ResolvedPrimitiveType to get type mapping. As you can see, type resolving also is deep enough so I suppose there is also no way to change it somehow.

Hope you find my findings helpful.

like image 148
Dmitry Senkovich Avatar answered Oct 26 '22 09:10

Dmitry Senkovich