Skip to content

Commit

Permalink
Use io.swagger.util.Yaml instead of io.swagger.v3.core.util.Yaml to add
Browse files Browse the repository at this point in the history
 quotes around string value and fix compatibility with parser using yaml 1.1.

Fix OpenAPITools#3196

In spec 1.1, some words without quotes can be implicitly converted to
 a boolean like "yes" or "on" (See https://yaml.org/type/bool.html)
In v3.core.util.Yaml with MINIMIZE_QUOTES enabled, the formatter created
 a YAML but forget to protect some words.

PRs are created to fix this in the formatter:
 - FasterXML/jackson-dataformats-text#137
 - FasterXML/jackson-dataformats-text#138

Until the PRs will be merged and released, this patch fix it by
re-adding quotes.
  • Loading branch information
GuillaumeSmaha committed Jun 28, 2019
1 parent 73966a0 commit 28de27a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.v3.core.util.Yaml;
import io.swagger.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.BooleanSchema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityRequirement;
Expand Down Expand Up @@ -39,53 +40,67 @@ public void testToYamlStringCompleteExample() throws Exception {
.description("Some description")
.operationId("pingOp")
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("Ok")))));
openAPI.components(new Components().addSchemas("SomeObject", new ObjectSchema().description("An Obj").addProperties("id", new StringSchema())));
openAPI.components(new Components().addSchemas("SomeObject", new ObjectSchema().description("An Obj")
.addProperties("id", new StringSchema())
.addProperties("stringBoolean", new StringSchema()._default("true"))
.addProperties("switch", new StringSchema()._default("on"))
.addProperties("valid", new BooleanSchema()._default(true))));
openAPI.setExtensions(new LinkedHashMap<>()); // required because swagger-core is using HashMap instead of LinkedHashMap internally.
openAPI.addExtension("x-custom", "value1");
openAPI.addExtension("x-other", "value2");

String content = SerializerUtils.toYamlString(openAPI);
String expected = "openapi: 3.0.1\n" +
String expected = "---\n" +
"openapi: \"3.0.1\"\n" +
"info:\n" +
" description: Some description\n" +
" title: Some title\n" +
" description: \"Some description\"\n" +
" title: \"Some title\"\n" +
"externalDocs:\n" +
" description: a-description\n" +
" url: http://abcdef.com\n" +
" description: \"a-description\"\n" +
" url: \"http://abcdef.com\"\n" +
"servers:\n" +
"- description: first server\n" +
" url: http://www.server1.com\n" +
"- description: second server\n" +
" url: http://www.server2.com\n" +
"- description: \"first server\"\n" +
" url: \"http://www.server1.com\"\n" +
"- description: \"second server\"\n" +
" url: \"http://www.server2.com\"\n" +
"security:\n" +
"- some_auth:\n" +
" - write\n" +
" - read\n" +
" - \"write\"\n" +
" - \"read\"\n" +
"tags:\n" +
"- description: some 1 description\n" +
" name: tag1\n" +
"- description: some 2 description\n" +
" name: tag2\n" +
"- description: some 3 description\n" +
" name: tag3\n" +
"- description: \"some 1 description\"\n" +
" name: \"tag1\"\n" +
"- description: \"some 2 description\"\n" +
" name: \"tag2\"\n" +
"- description: \"some 3 description\"\n" +
" name: \"tag3\"\n" +
"paths:\n" +
" /ping/pong:\n" +
" get:\n" +
" description: Some description\n" +
" operationId: pingOp\n" +
" description: \"Some description\"\n" +
" operationId: \"pingOp\"\n" +
" responses:\n" +
" 200:\n" +
" description: Ok\n" +
" description: \"Ok\"\n" +
"components:\n" +
" schemas:\n" +
" SomeObject:\n" +
" description: An Obj\n" +
" description: \"An Obj\"\n" +
" properties:\n" +
" id:\n" +
" type: string\n" +
" type: object\n" +
"x-custom: value1\n" +
"x-other: value2\n";
" type: \"string\"\n" +
" stringBoolean:\n" +
" default: \"true\"\n" +
" type: \"string\"\n" +
" switch:\n" +
" default: \"on\"\n" +
" type: \"string\"\n" +
" valid:\n" +
" default: true\n" +
" type: \"boolean\"\n" +
" type: \"object\"\n" +
"x-custom: \"value1\"\n" +
"x-other: \"value2\"\n";
assertEquals(content, expected);
}

Expand All @@ -102,19 +117,20 @@ public void testToYamlStringMinimalExample() throws Exception {
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("Ok")))));

String content = SerializerUtils.toYamlString(openAPI);
String expected = "openapi: 3.0.1\n" +
String expected = "---\n" +
"openapi: \"3.0.1\"\n" +
"info:\n" +
" title: Some title\n" +
" title: \"Some title\"\n" +
"servers:\n" +
"- url: http://www.server1.com\n" +
"- url: \"http://www.server1.com\"\n" +
"paths:\n" +
" /ping/pong:\n" +
" get:\n" +
" description: Some description\n" +
" operationId: pingOp\n" +
" description: \"Some description\"\n" +
" operationId: \"pingOp\"\n" +
" responses:\n" +
" 200:\n" +
" description: Ok\n";
" description: \"Ok\"\n";
assertEquals(content, expected);
}
}

0 comments on commit 28de27a

Please sign in to comment.