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

Support proper mapping of the byte array #3955

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,20 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
isPrimitive = true;
}
if (model == null) {
PrimitiveType primitiveType = PrimitiveType.fromType(type);
if (primitiveType != null) {
model = PrimitiveType.fromType(type).createProperty();
isPrimitive = true;
if (resolvedSchemaAnnotation != null && StringUtils.isEmpty(resolvedSchemaAnnotation.type())) {
PrimitiveType primitiveType = PrimitiveType.fromTypeAndFormat(type, resolvedSchemaAnnotation.format());
if (primitiveType != null) {
model = primitiveType.createProperty();
isPrimitive = true;
}
}

if (model == null) {
PrimitiveType primitiveType = PrimitiveType.fromType(type);
if (primitiveType != null) {
model = primitiveType.createProperty();
isPrimitive = true;
}
}
}

Expand Down Expand Up @@ -595,7 +605,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
propType = ((AnnotatedMethod)member).getParameterType(0);
}

}
}

String propSchemaName = null;
io.swagger.v3.oas.annotations.media.Schema ctxSchema = AnnotationsUtils.getSchemaAnnotation(annotations);
if (AnnotationsUtils.hasSchemaAnnotation(ctxSchema)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -149,6 +151,7 @@ public Schema createProperty() {
};

private static final Map<Class<?>, PrimitiveType> KEY_CLASSES;
private static final Map<Class<?>, Collection<PrimitiveType>> MULTI_KEY_CLASSES;
private static final Map<Class<?>, PrimitiveType> BASE_CLASSES;
/**
* Adds support of a small number of "well-known" types, specifically for
Expand Down Expand Up @@ -244,6 +247,11 @@ public Schema createProperty() {
addKeys(keyClasses, OBJECT, Object.class);
KEY_CLASSES = Collections.unmodifiableMap(keyClasses);

final Map<Class<?>, Collection<PrimitiveType>> multiKeyClasses = new HashMap<>();
addMultiKeys(multiKeyClasses, BYTE, byte[].class);
addMultiKeys(multiKeyClasses, BINARY, byte[].class);
MULTI_KEY_CLASSES = Collections.unmodifiableMap(multiKeyClasses);

final Map<Class<?>, PrimitiveType> baseClasses = new HashMap<>();
addKeys(baseClasses, DATE_TIME, java.util.Date.class, java.util.Calendar.class);
BASE_CLASSES = Collections.unmodifiableMap(baseClasses);
Expand Down Expand Up @@ -343,6 +351,20 @@ public static Set<String> nonSystemTypePackages() {
return nonSystemTypePackages;
}

public static PrimitiveType fromTypeAndFormat(Type type, String format) {
final Class<?> raw = TypeFactory.defaultInstance().constructType(type).getRawClass();
final Collection<PrimitiveType> keys = MULTI_KEY_CLASSES.get(raw);
if (keys == null || keys.isEmpty() || StringUtils.isBlank(format)) {
return fromType(type);
} else {
return keys
.stream()
.filter(t -> t.getCommonName().equalsIgnoreCase(format))
.findAny()
.orElse(null);
}
}

public static PrimitiveType fromType(Type type) {
final Class<?> raw = TypeFactory.defaultInstance().constructType(type).getRawClass();
final PrimitiveType key = KEY_CLASSES.get(raw);
Expand All @@ -351,6 +373,14 @@ public static PrimitiveType fromType(Type type) {
return key;
}
}

final Collection<PrimitiveType> keys = MULTI_KEY_CLASSES.get(raw);
if (keys != null && !keys.isEmpty()) {
final PrimitiveType first = keys.iterator().next();
if (!customExcludedClasses.contains(raw.getName())) {
return first;
}
}

final PrimitiveType custom = customClasses.get(raw.getName());
if (custom != null) {
Expand Down Expand Up @@ -424,6 +454,15 @@ private static <K> void addKeys(Map<K, PrimitiveType> map, PrimitiveType type, K
}
}

private static <K> void addMultiKeys(Map<K, Collection<PrimitiveType>> map, PrimitiveType type, K... keys) {
for (K key : keys) {
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(type);
}
}

private static class DateStub {
private DateStub() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.swagger.v3.jaxrs2;

import java.io.IOException;

import org.testng.annotations.Test;

import io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest;
import io.swagger.v3.jaxrs2.resources.BinaryParameterResource;

public class BinaryParameterResourceTest extends AbstractAnnotationTest {
@Test(description = "check binary model serialization") // tests issue #2466
public void shouldSerializeBinaryParameter() throws IOException {
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.swagger.v3.jaxrs2.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import io.swagger.v3.jaxrs2.resources.model.Item;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

@Path("/")
public class BinaryParameterResource {
@Consumes({ MediaType.APPLICATION_JSON })
@Path("/binary")
@POST
@Operation(
summary = "Create new item",
description = "Post operation with entity in a body",
responses = {
@ApiResponse(
content = @Content(
schema = @Schema(implementation = Item.class),
mediaType = MediaType.APPLICATION_JSON
),
headers = @Header(name = "Location"),
responseCode = "201"
)
}
)
public Response createItem(@Context final UriInfo uriInfo, @Parameter(required = true) final Item item) {
return Response
.created(uriInfo.getBaseUriBuilder().path(item.getName()).build())
.entity(item).build();
Dismissed Show dismissed Hide dismissed
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.swagger.v3.jaxrs2.resources.model;

import io.swagger.v3.oas.annotations.media.Schema;

public class Item {
private String name;
private String value;
@Schema(example = "Ynl0ZQ==")
private byte[] bytes;
@Schema(format = "binary", example = "YmluYXJ5")
private byte[] binary;

public Item() {
}

public void setBinary(byte[] binary) {
this.binary = binary;
}

public byte[] getBinary() {
return binary;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setBytes(byte[] bytes) {
this.bytes = bytes;
}

public byte[] getBytes() {
return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.1
paths:
/binary:
post:
summary: Create new item
description: Post operation with entity in a body
operationId: createItem
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
required: true
responses:
"201":
headers:
Location:
style: simple
content:
application/json:
schema:
$ref: '#/components/schemas/Item'
components:
schemas:
Item:
type: object
properties:
name:
type: string
value:
type: string
bytes:
type: string
format: byte
example: Ynl0ZQ==
binary:
type: string
format: binary
example: YmluYXJ5

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.v3.oas.models.media;

import java.util.Base64;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -36,6 +37,8 @@ protected byte[] cast(Object value) {
try {
if (value instanceof byte[]) {
return (byte[]) value;
} else if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
} else {
return value.toString().getBytes();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.v3.oas.models.media;

import java.util.Base64;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -36,6 +37,8 @@ protected byte[] cast(Object value) {
try {
if (value instanceof byte[]) {
return (byte[]) value;
} else if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
} else {
return value.toString().getBytes();
}
Expand Down