Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nullValues attribute in Schema and resolve it in ModelResolver #4794

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down