Skip to content

Commit

Permalink
Merge pull request #1682 from swagger-api/ticket-1630
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma authored Mar 23, 2022
2 parents 1f0ff86 + 10a4346 commit 46b522f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SchemaTypeUtil {
public static final String EMAIL_FORMAT = "email";
public static final String UUID_FORMAT = "uuid";

public static final String BINARY_AS_STRING = "swaggerParserBinaryAsString";

public static Schema createSchemaByType(ObjectNode node){
if(node == null) {
return new Schema();
Expand Down Expand Up @@ -78,9 +80,15 @@ else if(BOOLEAN_TYPE.equals(type)) {
}
else if(STRING_TYPE.equals(type)) {
if(BYTE_FORMAT.equals(format)) {
if (System.getProperty(BINARY_AS_STRING) != null || System.getenv(BINARY_AS_STRING) != null) {
return new StringSchema().format("byte");
}
return new ByteArraySchema();
}
else if(BINARY_FORMAT.equals(format)) {
if (System.getProperty(BINARY_AS_STRING) != null || System.getenv(BINARY_AS_STRING) != null) {
return new StringSchema().format("binary");
}
return new BinarySchema();
}
else if(DATE_FORMAT.equals(format)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Random;
import java.util.Set;

import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.io.FileUtils;
import org.hamcrest.CoreMatchers;
import org.testng.Assert;
Expand Down Expand Up @@ -84,6 +85,32 @@ public class OpenAPIV3ParserTest {
protected int serverPort = getDynamicPort();
protected WireMockServer wireMockServer;

@Test
public void testExampleFormatByte() throws Exception{

ParseOptions options = new ParseOptions();
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue1630.yaml", null, options);

Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
OpenAPI openAPI = result.getOpenAPI();
Schema model = (Schema) openAPI.getComponents().getSchemas().get("Response").getProperties().get("content");
assertTrue(model instanceof ByteArraySchema);
ByteArraySchema byteArraySchema = (ByteArraySchema) model;
assertEquals(new String((byte[])byteArraySchema.getExample()), "VGhpc1Nob3VsZFBhc3MK");
System.setProperty(SchemaTypeUtil.BINARY_AS_STRING, "true");
result = new OpenAPIV3Parser().readLocation("src/test/resources/issue1630.yaml", null, options);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
openAPI = result.getOpenAPI();
model = (Schema) openAPI.getComponents().getSchemas().get("Response").getProperties().get("content");
assertTrue(model instanceof StringSchema);
StringSchema stringSchema = (StringSchema) model;
assertEquals(stringSchema.getExample(), "VGhpc1Nob3VsZFBhc3MK");
System.clearProperty(SchemaTypeUtil.BINARY_AS_STRING);

}

@Test
public void testIssue1658() throws Exception{
ParseOptions options = new ParseOptions();
Expand Down
20 changes: 20 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue1630.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
openapi: 3.0.0
info:
title: example
version: 1.0.0
paths:
"/examples":
get:
responses:
default:
description: None
components:
schemas:
Response:
required:
- content
properties:
content:
type: string
format: byte
example: "VGhpc1Nob3VsZFBhc3MK"
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,15 @@
<swagger-parser-v2-version>1.0.57</swagger-parser-v2-version>
<commons-io-version>2.11.0</commons-io-version>
<slf4j-version>1.7.30</slf4j-version>
<swagger-core-version>2.1.13</swagger-core-version>
<swagger-core-version>2.2.0-SNAPSHOT</swagger-core-version>
<swagger-core-v2-version>1.6.5</swagger-core-v2-version>
<junit-version>4.13.2</junit-version>
<testng-version>6.14.2</testng-version>
<jmockit-version>1.35</jmockit-version>
<wiremock-version>2.15.0</wiremock-version>
<surefire-version>2.22.2</surefire-version>
<commons-lang-version>3.2.1</commons-lang-version>
<jackson-version>2.12.6</jackson-version>
<jackson-version>2.13.2</jackson-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonatypeOssDistMgmtSnapshotsUrl>https://oss.sonatype.org/content/repositories/snapshots/</sonatypeOssDistMgmtSnapshotsUrl>
</properties>
Expand Down

0 comments on commit 46b522f

Please sign in to comment.