Make Swagger use primitive int and boolean in generated models
Asked Answered
E

1

15

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.

Eadith answered 7/7, 2017 at 12:1 Comment(4)
Are you using springfox?Akeyla
@DmitrySenkovich No, and I don't know what it isEadith
I decided to make a research for both cases anyway:)Akeyla
Why do you care about Spring initializing fields to null, when the whole request should be deemed invalid and thrown out?Allegraallegretto
A
11

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.

Akeyla answered 12/7, 2017 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.