Skip to content

Improved support for empty response definition and response schema ge… #3872

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -23,7 +23,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.file.AccessMode;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
Expand Down Expand Up @@ -335,4 +334,14 @@ enum AccessMode {
WRITE_ONLY,
READ_WRITE;
}

/**
* Marker class to use on the {@link Schema} implementation field to set that a
* response has no content.
*
* @author Ariel Carrera
*
*/
public static class Empty {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.swagger.v3.oas.annotations.links.LinkParameter;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema.Empty;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.examples.Example;
Expand Down Expand Up @@ -540,12 +541,17 @@ public static Optional<Schema> getSchemaFromAnnotation(io.swagger.v3.oas.annotat
}
}


if (schemaObject.isEmpty()) {
return Optional.of(schemaObject.noContent(true));
}
return Optional.of(schemaObject);
}

public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Components components, JsonView jsonViewAnnotation) {
Schema schemaObject;
if (Empty.class.equals(schemaImplementation)) {
return new Schema().noContent(true);
}
Schema schemaObject;
PrimitiveType primitiveType = PrimitiveType.fromType(schemaImplementation);
if (primitiveType != null) {
schemaObject = primitiveType.createProperty();
Expand Down Expand Up @@ -1088,6 +1094,8 @@ public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations
if (schemaImplementation != Void.class) {
isArray = true;
}
} else if (schemaImplementation == Empty.class) {
return Optional.of(new Schema<>().noContent(true));
}
return getSchema(annotationContent.schema(), annotationContent.array(), isArray, schemaImplementation, components, jsonViewAnnotation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,38 @@ protected Operation parseMethod(
}
}
}
operation.getResponses().forEach((k, value) -> {
if (value.getContent() == null) { // no content annotation -> DEFAULT CONTENT/SCHEMA
value.setContent(content);
} else if (!"default".equals(k)) {
Set<String> contentsToRemove = new HashSet<>();
value.getContent().forEach((mediaStr, mediaObj) -> {
if (mediaObj.isEmpty()) { // content annotation empty or only mediaType field defined -> DEFAULT CONTENT/SCHEMA
if (value.getContent().size() == 1) {
value.setContent(new Content().addMediaType(mediaStr, mediaType));
} else {
mediaObj.setSchema(returnTypeSchema);
}
} else {
Schema schemaObj = mediaObj.getSchema();
if (schemaObj == null || schemaObj.isEmpty()) {
mediaObj.setSchema(returnTypeSchema);
} else if (schemaObj.isNoContent()) {
mediaObj.setSchema(null);
if (mediaObj.isEmpty()) {
contentsToRemove.add(mediaStr);
}
}
}
});

contentsToRemove.stream().forEach(s -> value.getContent().remove(s));
if (value.getContent().isEmpty()) {
value.setContent(null);
}
}
});

Map<String, Schema> schemaMap = resolvedSchema.referencedSchemas;
if (schemaMap != null) {
schemaMap.forEach((key, schema) -> components.addSchemas(key, schema));
Expand All @@ -1108,7 +1140,7 @@ protected Operation parseMethod(

return operation;
}

private boolean shouldIgnoreClass(String className) {
if (StringUtils.isBlank(className)) {
return true;
Expand Down
Loading