Skip to content

Commit

Permalink
refs #3944 - add option to resolve byte[] into StringSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Apr 19, 2024
1 parent 8e20f11 commit 616350b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@ public Schema createProperty() {
},
BYTE(Byte.class, "byte") {
@Override
public ByteArraySchema createProperty() {
public Schema createProperty() {
if (
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
return new StringSchema().format("byte");
}
return new ByteArraySchema();
}
},
BINARY(Byte.class, "binary") {
@Override
public BinarySchema createProperty() {
public Schema createProperty() {
if (
(System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString())) ||
(System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(Schema.BINARY_STRING_CONVERSION_PROPERTY).equals(Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString()))) {
return new StringSchema().format("binary");
}
return new BinarySchema();
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@

import java.io.IOException;

import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
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"));

@Test(description = "check binary model serialization with base64", singleThreaded = true) // tests issue #2466
public void shouldSerializeBinaryParameterBase64() throws IOException {
try {
System.setProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY, Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString());
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
} finally {
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
}
}

@BeforeTest
public void before() {
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
}

@AfterTest
public void after() {
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
}


@Test(description = "check binary model serialization with StringSchema", singleThreaded = true) // tests issue #2466
public void shouldSerializeBinaryParameterStringSchema() throws IOException {
try {
System.setProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY, Schema.BynaryStringConversion.BINARY_STRING_CONVERSION_STRING_SCHEMA.toString());
compareAsYaml(BinaryParameterResource.class, getOpenAPIAsString("BinaryParameterResource.yaml"));
} finally {
System.clearProperty(Schema.BINARY_STRING_CONVERSION_PROPERTY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class Item {
private byte[] bytes;
@Schema(format = "binary", example = "YmluYXJ5")
private byte[] binary;


private byte[] byteNoAnnotation;

public Item() {
}

Expand Down Expand Up @@ -44,4 +46,12 @@ public void setBytes(byte[] bytes) {
public byte[] getBytes() {
return bytes;
}

public void setByteNoAnnotation(byte[] byteNoAnnotation) {
this.byteNoAnnotation = byteNoAnnotation;
}

public byte[] getByteNoAnnotation() {
return byteNoAnnotation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ components:
type: string
format: binary
example: YmluYXJ5

byteNoAnnotation:
type: string
format: byte
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ protected byte[] cast(Object value) {
try {
if (value instanceof byte[]) {
return (byte[]) value;
} else if (value instanceof String) {
if (
(System.getProperty(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString())) ||
(System.getenv(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString()))) {
return Base64.getDecoder().decode((String) value);
}
return value.toString().getBytes();
} else {
return value.toString().getBytes();
}
if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
}
} catch (Exception e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ protected byte[] cast(Object value) {
try {
if (value instanceof byte[]) {
return (byte[]) value;
} else if (value instanceof String) {
if (
(System.getProperty(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getProperty(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString())) ||
(System.getenv(BINARY_STRING_CONVERSION_PROPERTY) != null && System.getenv(BINARY_STRING_CONVERSION_PROPERTY).equals(BynaryStringConversion.BINARY_STRING_CONVERSION_BASE64.toString()))) {
return Base64.getDecoder().decode((String) value);
}
return value.toString().getBytes();
} else {
return value.toString().getBytes();
}
if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
}
} catch (Exception e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
public class Schema<T> {

public static final String BIND_TYPE_AND_TYPES = "bind-type";
public static final String BINARY_STRING_CONVERSION_PROPERTY = "binary-string-conversion";
public enum BynaryStringConversion {
BINARY_STRING_CONVERSION_BASE64("base64"),
BINARY_STRING_CONVERSION_DEFAULT_CHARSET("default"),
BINARY_STRING_CONVERSION_STRING_SCHEMA("string-schema");
private String value;

BynaryStringConversion(String value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(value);
}
}

protected T _default;

Expand Down

0 comments on commit 616350b

Please sign in to comment.