Skip to content

Commit 10a4346

Browse files
committed
refs #1630 - add option to deserialize byte/binary schemas as StringSchema
1 parent 1f0ff86 commit 10a4346

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class SchemaTypeUtil {
4040
public static final String EMAIL_FORMAT = "email";
4141
public static final String UUID_FORMAT = "uuid";
4242

43+
public static final String BINARY_AS_STRING = "swaggerParserBinaryAsString";
44+
4345
public static Schema createSchemaByType(ObjectNode node){
4446
if(node == null) {
4547
return new Schema();
@@ -78,9 +80,15 @@ else if(BOOLEAN_TYPE.equals(type)) {
7880
}
7981
else if(STRING_TYPE.equals(type)) {
8082
if(BYTE_FORMAT.equals(format)) {
83+
if (System.getProperty(BINARY_AS_STRING) != null || System.getenv(BINARY_AS_STRING) != null) {
84+
return new StringSchema().format("byte");
85+
}
8186
return new ByteArraySchema();
8287
}
8388
else if(BINARY_FORMAT.equals(format)) {
89+
if (System.getProperty(BINARY_AS_STRING) != null || System.getenv(BINARY_AS_STRING) != null) {
90+
return new StringSchema().format("binary");
91+
}
8492
return new BinarySchema();
8593
}
8694
else if(DATE_FORMAT.equals(format)) {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Random;
3232
import java.util.Set;
3333

34+
import io.swagger.v3.parser.util.SchemaTypeUtil;
3435
import org.apache.commons.io.FileUtils;
3536
import org.hamcrest.CoreMatchers;
3637
import org.testng.Assert;
@@ -84,6 +85,32 @@ public class OpenAPIV3ParserTest {
8485
protected int serverPort = getDynamicPort();
8586
protected WireMockServer wireMockServer;
8687

88+
@Test
89+
public void testExampleFormatByte() throws Exception{
90+
91+
ParseOptions options = new ParseOptions();
92+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue1630.yaml", null, options);
93+
94+
Assert.assertNotNull(result);
95+
Assert.assertNotNull(result.getOpenAPI());
96+
OpenAPI openAPI = result.getOpenAPI();
97+
Schema model = (Schema) openAPI.getComponents().getSchemas().get("Response").getProperties().get("content");
98+
assertTrue(model instanceof ByteArraySchema);
99+
ByteArraySchema byteArraySchema = (ByteArraySchema) model;
100+
assertEquals(new String((byte[])byteArraySchema.getExample()), "VGhpc1Nob3VsZFBhc3MK");
101+
System.setProperty(SchemaTypeUtil.BINARY_AS_STRING, "true");
102+
result = new OpenAPIV3Parser().readLocation("src/test/resources/issue1630.yaml", null, options);
103+
Assert.assertNotNull(result);
104+
Assert.assertNotNull(result.getOpenAPI());
105+
openAPI = result.getOpenAPI();
106+
model = (Schema) openAPI.getComponents().getSchemas().get("Response").getProperties().get("content");
107+
assertTrue(model instanceof StringSchema);
108+
StringSchema stringSchema = (StringSchema) model;
109+
assertEquals(stringSchema.getExample(), "VGhpc1Nob3VsZFBhc3MK");
110+
System.clearProperty(SchemaTypeUtil.BINARY_AS_STRING);
111+
112+
}
113+
87114
@Test
88115
public void testIssue1658() throws Exception{
89116
ParseOptions options = new ParseOptions();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
openapi: 3.0.0
2+
info:
3+
title: example
4+
version: 1.0.0
5+
paths:
6+
"/examples":
7+
get:
8+
responses:
9+
default:
10+
description: None
11+
components:
12+
schemas:
13+
Response:
14+
required:
15+
- content
16+
properties:
17+
content:
18+
type: string
19+
format: byte
20+
example: "VGhpc1Nob3VsZFBhc3MK"

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,15 @@
397397
<swagger-parser-v2-version>1.0.57</swagger-parser-v2-version>
398398
<commons-io-version>2.11.0</commons-io-version>
399399
<slf4j-version>1.7.30</slf4j-version>
400-
<swagger-core-version>2.1.13</swagger-core-version>
400+
<swagger-core-version>2.2.0-SNAPSHOT</swagger-core-version>
401401
<swagger-core-v2-version>1.6.5</swagger-core-v2-version>
402402
<junit-version>4.13.2</junit-version>
403403
<testng-version>6.14.2</testng-version>
404404
<jmockit-version>1.35</jmockit-version>
405405
<wiremock-version>2.15.0</wiremock-version>
406406
<surefire-version>2.22.2</surefire-version>
407407
<commons-lang-version>3.2.1</commons-lang-version>
408-
<jackson-version>2.12.6</jackson-version>
408+
<jackson-version>2.13.2</jackson-version>
409409
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
410410
<sonatypeOssDistMgmtSnapshotsUrl>https://oss.sonatype.org/content/repositories/snapshots/</sonatypeOssDistMgmtSnapshotsUrl>
411411
</properties>

0 commit comments

Comments
 (0)