diff --git a/modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/media/Schema.java b/modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/media/Schema.java index 0ba732c6e5..76d42197b1 100644 --- a/modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/media/Schema.java +++ b/modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/media/Schema.java @@ -255,6 +255,13 @@ **/ String example() default ""; + /** + * Set value of example to null if array contains the value of example + * + * @return Array of string that each element must be set to null if it equals example + */ + String[] nullValues() default {}; + /** * Additional external documentation for this schema. * diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index 2e5ea01248..5b67d64984 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -2035,6 +2035,18 @@ protected Object resolveExample(Annotated a, Annotation[] annotations, io.swagge return null; } + protected Boolean resolveNullValues(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) { + if (schema != null) { + for (String nullValue : schema.nullValues()) { + if (nullValue.equals(schema.example())) { + return true; + } + } + } + + return false; + } + protected io.swagger.v3.oas.annotations.media.Schema.RequiredMode resolveRequiredMode(io.swagger.v3.oas.annotations.media.Schema schema) { if (schema != null && !schema.requiredMode().equals(io.swagger.v3.oas.annotations.media.Schema.RequiredMode.AUTO)) { return schema.requiredMode(); @@ -2801,6 +2813,10 @@ protected void resolveSchemaMembers(Schema schema, Annotated a, Annotation[] ann if (example != null) { schema.example(example); } + Boolean nullValue = resolveNullValues(a, annotations, schemaAnnotation); + if (nullValue) { + schema.example(null); + } Boolean readOnly = resolveReadOnly(a, annotations, schemaAnnotation); if (readOnly != null) { schema.readOnly(readOnly); diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java index cb26009e0a..0ba69396ab 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java @@ -2309,6 +2309,14 @@ public String example() { return patch.example(); } + @Override + public String[] nullValues() { + if (master.nullValues().length > 0 || patch.nullValues().length == 0) { + return master.nullValues(); + } + return patch.nullValues(); + } + @Override public io.swagger.v3.oas.annotations.ExternalDocumentation externalDocs() { if (getExternalDocumentation(master.externalDocs()).isPresent() || !getExternalDocumentation(patch.externalDocs()).isPresent()) {