Skip to content
New issue

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

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

Already on GitHub? # to your account

Bugfix/request body annotation #2401

Merged
merged 2 commits into from
Aug 25, 2017
Merged
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 @@ -202,7 +202,7 @@ public static Schema fillSchema(Schema schema, Type type) {
Schema inner = pt.createProperty();
return merge(schema, inner);
} else {
return ModelConverters.getInstance().resolveProperty(type);
return merge(schema, ModelConverters.getInstance().resolveProperty(type));
}
} else if ("array".equals(schema.getType())) {
Schema inner = fillSchema(((ArraySchema) schema).getItems(), type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import io.swagger.oas.models.servers.ServerVariable;
import io.swagger.oas.models.servers.ServerVariables;
import io.swagger.oas.models.tags.Tag;
import io.swagger.util.ParameterProcessor;
import io.swagger.util.Json;
import io.swagger.util.ParameterProcessor;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.Produces;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand All @@ -43,7 +42,6 @@
*/
public class OperationParser {

public static final String RESPONSE_DEFAULT = "default";
public static final String MEDIA_TYPE = "*/*";
public static final String COMPONENTS_REF = "#/components/schemas/";
public static final String DEFAULT_DESCRIPTION = "no description";
Expand Down Expand Up @@ -343,9 +341,9 @@ public static Optional<Content> getContents(io.swagger.oas.annotations.media.Con
Content contentObject = new Content();
MediaType mediaType = new MediaType();
for (io.swagger.oas.annotations.media.Content annotationContent : annotationContents) {
ExampleObject[] examples = annotationContent.examples();
for (ExampleObject example : examples) {
getMediaType(mediaType, example).ifPresent(mediaTypeObject -> contentObject.addMediaType(annotationContent.mediaType(), mediaType));
getSchema(annotationContent, components).ifPresent(mediaType::setSchema);
if (mediaType.getSchema() != null) {
contentObject.addMediaType(MEDIA_TYPE, mediaType);
}
}
if (contentObject.size() == 0) {
Expand All @@ -364,52 +362,66 @@ public static Optional<Content> getContent(io.swagger.oas.annotations.media.Cont

for (io.swagger.oas.annotations.media.Content annotationContent : annotationContents) {
MediaType mediaType = new MediaType();
Class<?> schemaImplementation = annotationContent.schema().implementation();
Map<String, Schema> schemaMap;
if (schemaImplementation != Void.class) {
Schema schemaObject = new Schema();
if (schemaImplementation.getName().startsWith("java.lang")) {
schemaObject.setType(schemaImplementation.getSimpleName().toLowerCase());
} else {
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
schemaMap.forEach((key, schema) -> {
components.addSchemas(key, schema);
});
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
}
mediaType.setSchema(schemaObject);
getSchema(annotationContent, components).ifPresent(mediaType::setSchema);

} else {
getSchemaFromAnnotation(annotationContent.schema()).ifPresent(mediaType::setSchema);
}
if (StringUtils.isNotBlank(annotationContent.mediaType())) {
content.addMediaType(annotationContent.mediaType(), mediaType);
} else {
if (mediaType.getSchema() != null) {
if (methodProduces != null) {
for (String value : methodProduces.value()) {
content.addMediaType(value, mediaType);
}
} else if (classProduces != null) {
for (String value : classProduces.value()) {
content.addMediaType(value, mediaType);
}
} else {
content.addMediaType(MEDIA_TYPE, mediaType);
}
applyProduces(classProduces, methodProduces, content, mediaType);
}
}
ExampleObject[] examples = annotationContent.examples();
for (ExampleObject example : examples) {
getMediaType(mediaType, example).ifPresent(mediaTypeObject -> content.addMediaType(annotationContent.mediaType(), mediaTypeObject));
}
}
}
if (content.size() == 0) {
return Optional.empty();
}
return Optional.of(content);
}

public static void applyProduces(Produces classProduces, Produces methodProduces, Content content, MediaType mediaType) {
if (methodProduces != null) {
for (String value : methodProduces.value()) {
content.addMediaType(value, mediaType);
}
} else if (classProduces != null) {
for (String value : classProduces.value()) {
content.addMediaType(value, mediaType);
}
} else {
content.addMediaType(MEDIA_TYPE, mediaType);
}

}

public static Optional<Schema> getSchema(io.swagger.oas.annotations.media.Content annotationContent, Components components) {
Class<?> schemaImplementation = annotationContent.schema().implementation();
Map<String, Schema> schemaMap;
if (schemaImplementation != Void.class) {
Schema schemaObject = new Schema();
if (schemaImplementation.getName().startsWith("java.lang")) {
schemaObject.setType(schemaImplementation.getSimpleName().toLowerCase());
} else {
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
schemaMap.forEach((key, schema) -> {
components.addSchemas(key, schema);
});
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
}
return Optional.of(schemaObject);

} else {
Optional<Schema> schemaFromAnnotation = getSchemaFromAnnotation(annotationContent.schema());
if (schemaFromAnnotation.isPresent()) {
return Optional.of(schemaFromAnnotation.get());
}
}
return Optional.empty();
}

public static Optional<MediaType> getMediaType(MediaType mediaType, ExampleObject example) {
if (example == null) {
return Optional.empty();
Expand Down
Loading