Skip to content

Commit 616350b

Browse files
committed
refs #3944 - add option to resolve byte[] into StringSchema
1 parent 8e20f11 commit 616350b

File tree

7 files changed

+89
-13
lines changed

7 files changed

+89
-13
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/util/PrimitiveType.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,23 @@ public Schema createProperty() {
4343
},
4444
BYTE(Byte.class, "byte") {
4545
@Override
46-
public ByteArraySchema createProperty() {
46+
public Schema createProperty() {
47+
if (
48+
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
49+
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
50+
return new StringSchema().format("byte");
51+
}
4752
return new ByteArraySchema();
4853
}
4954
},
5055
BINARY(Byte.class, "binary") {
5156
@Override
52-
public BinarySchema createProperty() {
57+
public Schema createProperty() {
58+
if (
59+
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
60+
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
61+
return new StringSchema().format("binary");
62+
}
5363
return new BinarySchema();
5464
}
5565
},

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/BinaryParameterResourceTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
22

33
import java.io.IOException;
44

5+
import io.swagger.v3.oas.models.media.Schema;
6+
import org.testng.annotations.AfterTest;
7+
import org.testng.annotations.BeforeTest;
58
import org.testng.annotations.Test;
69

710
import io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest;
811
import io.swagger.v3.jaxrs2.resources.BinaryParameterResource;
912

1013
public class BinaryParameterResourceTest extends AbstractAnnotationTest {
11-
@Test(description = "check binary model serialization") // tests issue #2466
12-
public void shouldSerializeBinaryParameter() throws IOException {
13-
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
14+
15+
@Test(description = "check binary model serialization with base64", singleThreaded = true) // tests issue #2466
16+
public void shouldSerializeBinaryParameterBase64() throws IOException {
17+
try {
18+
System.setProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY, Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString());
19+
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
20+
} finally {
21+
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
22+
}
23+
}
24+
25+
@BeforeTest
26+
public void before() {
27+
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
28+
}
29+
30+
@AfterTest
31+
public void after() {
32+
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
33+
}
34+
35+
36+
@Test(description = "check binary model serialization with StringSchema", singleThreaded = true) // tests issue #2466
37+
public void shouldSerializeBinaryParameterStringSchema() throws IOException {
38+
try {
39+
System.setProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY, Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString());
40+
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
41+
} finally {
42+
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
43+
}
1444
}
1545
}

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/model/Item.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class Item {
99
private byte[] bytes;
1010
@Schema(format = "binary", example = "YmluYXJ5")
1111
private byte[] binary;
12-
12+
13+
private byte[] byteNoAnnotation;
14+
1315
public Item() {
1416
}
1517

@@ -44,4 +46,12 @@ public void setBytes(byte[] bytes) {
4446
public byte[] getBytes() {
4547
return bytes;
4648
}
49+
50+
public void setByteNoAnnotation(byte[] byteNoAnnotation) {
51+
this.byteNoAnnotation = byteNoAnnotation;
52+
}
53+
54+
public byte[] getByteNoAnnotation() {
55+
return byteNoAnnotation;
56+
}
4757
}

modules/swagger-jaxrs2/src/test/resources/BinaryParameterResource.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ components:
3737
type: string
3838
format: binary
3939
example: YmluYXJ5
40-
40+
byteNoAnnotation:
41+
type: string
42+
format: byte

modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/BinarySchema.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ protected byte[] cast(Object value) {
3737
try {
3838
if (value instanceof byte[]) {
3939
return (byte[]) value;
40+
} else if (value instanceof String) {
41+
if (
42+
(System.getProperty(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString())) ||
43+
(System.getenv(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString()))) {
44+
return Base64.getDecoder().decode((String) value);
45+
}
46+
return value.toString().getBytes();
4047
} else {
4148
return value.toString().getBytes();
4249
}
43-
if (value instanceof String) {
44-
return Base64.getDecoder().decode((String) value);
45-
}
4650
} catch (Exception e) {
4751
}
4852
}

modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/ByteArraySchema.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ protected byte[] cast(Object value) {
3737
try {
3838
if (value instanceof byte[]) {
3939
return (byte[]) value;
40+
} else if (value instanceof String) {
41+
if (
42+
(System.getProperty(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString())) ||
43+
(System.getenv(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString()))) {
44+
return Base64.getDecoder().decode((String) value);
45+
}
46+
return value.toString().getBytes();
4047
} else {
4148
return value.toString().getBytes();
4249
}
43-
if (value instanceof String) {
44-
return Base64.getDecoder().decode((String) value);
45-
}
4650
} catch (Exception e) {
4751
}
4852
}

modules/swagger-models/src/main/java/io/swagger/v3/oas/models/media/Schema.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
public class Schema<T> {
2828

2929
public static final String BIND_TYPE_AND_TYPES = "bind-type";
30+
public static final String BINARY_STRING_CONVERSION_PROPERTY = "binary-string-conversion";
31+
public enum BynaryStringConversion {
32+
BINARY_STRING_CONVERSION_BASE64("base64"),
33+
BINARY_STRING_CONVERSION_DEFAULT_CHARSET("default"),
34+
BINARY_STRING_CONVERSION_STRING_SCHEMA("string-schema");
35+
private String value;
36+
37+
BynaryStringConversion(String value) {
38+
this.value = value;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return String.valueOf(value);
44+
}
45+
}
3046

3147
protected T _default;
3248

0 commit comments

Comments
 (0)