diff --git a/src/main/java/io/github/swagger2markup/internal/utils/ExamplesUtil.java b/src/main/java/io/github/swagger2markup/internal/utils/ExamplesUtil.java index 30e95a68..fa8e0ad5 100644 --- a/src/main/java/io/github/swagger2markup/internal/utils/ExamplesUtil.java +++ b/src/main/java/io/github/swagger2markup/internal/utils/ExamplesUtil.java @@ -124,6 +124,9 @@ public static Map generateRequestExampleMap(boolean generateMiss if (generateMissingExamples) { Object abstractSerializableParameterExample; abstractSerializableParameterExample = ((AbstractSerializableParameter) parameter).getExample(); + if (abstractSerializableParameterExample == null) { + abstractSerializableParameterExample = parameter.getVendorExtensions().get("x-example"); + } if (abstractSerializableParameterExample == null) { Property item = ((AbstractSerializableParameter) parameter).getItems(); if (item != null) { diff --git a/src/test/java/io/github/swagger2markup/internal/component/PathOperationComponentTest.java b/src/test/java/io/github/swagger2markup/internal/component/PathOperationComponentTest.java index bcf33fb0..60267614 100644 --- a/src/test/java/io/github/swagger2markup/internal/component/PathOperationComponentTest.java +++ b/src/test/java/io/github/swagger2markup/internal/component/PathOperationComponentTest.java @@ -15,8 +15,10 @@ */ package io.github.swagger2markup.internal.component; +import io.github.swagger2markup.Swagger2MarkupConfig; import io.github.swagger2markup.Swagger2MarkupConverter; import io.github.swagger2markup.assertions.DiffUtils; +import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder; import io.github.swagger2markup.internal.resolver.DefinitionDocumentResolverFromOperation; import io.github.swagger2markup.internal.resolver.SecurityDocumentResolver; import io.github.swagger2markup.internal.utils.PathUtils; @@ -30,7 +32,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class PathOperationComponentTest extends AbstractComponentTest { @@ -96,5 +100,72 @@ public void testInlineSchema() throws URISyntaxException { } + @Test + public void testWithPathParamExample() throws URISyntaxException { + String COMPONENT_NAME = "path_operation_with_path_param_example"; + Path outputDirectory = getOutputFile(COMPONENT_NAME); + FileUtils.deleteQuietly(outputDirectory.toFile()); + + Map configMap = new HashMap<>(); + configMap.put("swagger2markup.generatedExamplesEnabled", "true"); // enable example + + //Given + Path file = Paths.get(PathOperationComponentTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); + Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder(configMap).build(); + Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).withConfig(config) .build(); + Swagger swagger = converter.getContext().getSwagger(); + + io.swagger.models.Path path = swagger.getPaths().get("/pets/{petId}"); + List pathOperations = PathUtils.toPathOperationsList("/pets/{petId}", path); + + Swagger2MarkupConverter.Context context = converter.getContext(); + MarkupDocBuilder markupDocBuilder = context.createMarkupDocBuilder(); + + //When + markupDocBuilder = new PathOperationComponent(context, + new DefinitionDocumentResolverFromOperation(context), + new SecurityDocumentResolver(context)). + apply(markupDocBuilder, PathOperationComponent.parameters(pathOperations.get(0))); + + markupDocBuilder.writeToFileWithoutExtension(outputDirectory, StandardCharsets.UTF_8); + + //Then + Path expectedFile = getExpectedFile(COMPONENT_NAME); + DiffUtils.assertThatFileIsEqual(expectedFile, outputDirectory, getReportName(COMPONENT_NAME)); + } + + @Test + public void testWithQueryParamExample() throws URISyntaxException { + String COMPONENT_NAME = "path_operation_with_query_param_example"; + Path outputDirectory = getOutputFile(COMPONENT_NAME); + FileUtils.deleteQuietly(outputDirectory.toFile()); + + Map configMap = new HashMap<>(); + configMap.put("swagger2markup.generatedExamplesEnabled", "true"); // enable example + + //Given + Path file = Paths.get(PathOperationComponentTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); + Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder(configMap).build(); + Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).withConfig(config) .build(); + Swagger swagger = converter.getContext().getSwagger(); + + io.swagger.models.Path path = swagger.getPaths().get("/pets/findByTags"); + List pathOperations = PathUtils.toPathOperationsList("/pets/findByTags", path); + + Swagger2MarkupConverter.Context context = converter.getContext(); + MarkupDocBuilder markupDocBuilder = context.createMarkupDocBuilder(); + + //When + markupDocBuilder = new PathOperationComponent(context, + new DefinitionDocumentResolverFromOperation(context), + new SecurityDocumentResolver(context)). + apply(markupDocBuilder, PathOperationComponent.parameters(pathOperations.get(0))); + + markupDocBuilder.writeToFileWithoutExtension(outputDirectory, StandardCharsets.UTF_8); + + //Then + Path expectedFile = getExpectedFile(COMPONENT_NAME); + DiffUtils.assertThatFileIsEqual(expectedFile, outputDirectory, getReportName(COMPONENT_NAME)); + } } diff --git a/src/test/resources/component/path_operation_with_path_param_example.adoc b/src/test/resources/component/path_operation_with_path_param_example.adoc new file mode 100644 index 00000000..c7b98f62 --- /dev/null +++ b/src/test/resources/component/path_operation_with_path_param_example.adoc @@ -0,0 +1,89 @@ + +[[_getpetbyid]] +=== Find pet by ID +.... +GET /pets/{petId} +.... + + +==== Description +Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions + + +==== Parameters + +[options="header", cols=".^2,.^3,.^9,.^4"] +|=== +|Type|Name|Description|Schema +|**Path**|**petId** + +__required__|ID of pet that needs to be fetched|integer (int64) +|=== + + +==== Responses + +[options="header", cols=".^2,.^14,.^4"] +|=== +|HTTP Code|Description|Schema +|**200**|successful operation + +**Headers** : + +`X-Rate-Limit-Limit` (integer) : The number of allowed requests in the current period. + +`X-Rate-Limit-Remaining` (integer) : The number of remaining requests in the current period. + +`X-Rate-Limit-Reset` (integer) : The number of seconds left in the current period.|<<_pet,Pet>> +|**400**|Invalid ID supplied|No Content +|**404**|Pet not found|No Content +|=== + + +==== Produces + +* `application/json` +* `application/xml` + + +==== Tags + +* pet + + +==== Security + +[options="header", cols=".^3,.^4,.^13"] +|=== +|Type|Name|Scopes +|**apiKey**|**<<_api_key,api_key>>**| +|**oauth2**|**<<_petstore_auth,petstore_auth>>**|write_pets,read_pets +|=== + + +==== Example HTTP request + +===== Request path +---- +/pets/30 +---- + + +==== Example HTTP response + +===== Response 200 +[source,json] +---- +{ + "id" : 0, + "category" : { + "id" : 0, + "name" : "FoobarCategory" + }, + "name" : "doggie", + "photoUrls" : [ "string" ], + "tags" : [ { + "id" : 0, + "name" : "string" + } ], + "status" : "string" +} +---- + + + diff --git a/src/test/resources/component/path_operation_with_query_param_example.adoc b/src/test/resources/component/path_operation_with_query_param_example.adoc new file mode 100644 index 00000000..f53e6bcd --- /dev/null +++ b/src/test/resources/component/path_operation_with_query_param_example.adoc @@ -0,0 +1,96 @@ + +[[_findpetsbytags]] +=== Finds Pets by tags +.... +GET /pets/findByTags +.... + + +==== Description +Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. + + +==== Parameters + +[options="header", cols=".^2,.^3,.^9,.^4"] +|=== +|Type|Name|Description|Schema +|**Query**|**tags** + +__optional__|Tags to filter by|< string > array(multi) +|=== + + +==== Responses + +[options="header", cols=".^2,.^14,.^4"] +|=== +|HTTP Code|Description|Schema +|**200**|successful operation + +**Headers** : + +`X-Rate-Limit-Limit` (integer) : The number of allowed requests in the current period. + +`X-Rate-Limit-Remaining` (integer) : The number of remaining requests in the current period. + +`X-Rate-Limit-Reset` (integer) : The number of seconds left in the current period.|< <<_pet,Pet>> > array +|**400**|Invalid tag value|No Content +|=== + + +==== Produces + +* `application/json` +* `application/xml` + + +==== Tags + +* pet + + +==== Security + +[options="header", cols=".^3,.^4,.^13"] +|=== +|Type|Name|Scopes +|**oauth2**|**<<_petstore_auth,petstore_auth>>**|write_pets,read_pets +|=== + + +==== Example HTTP request + +===== Request path +---- +/pets/findByTags +---- + + +===== Request query +[source,json] +---- +{ + "tags" : "adorable" +} +---- + + +==== Example HTTP response + +===== Response 200 +[source,json] +---- +[ { + "id" : 0, + "category" : { + "id" : 0, + "name" : "FoobarCategory" + }, + "name" : "doggie", + "photoUrls" : [ "string" ], + "tags" : [ { + "id" : 0, + "name" : "string" + } ], + "status" : "string" +} ] +---- + + + diff --git a/src/test/resources/yaml/swagger_petstore.yaml b/src/test/resources/yaml/swagger_petstore.yaml index b3ec6560..69ee93ea 100644 --- a/src/test/resources/yaml/swagger_petstore.yaml +++ b/src/test/resources/yaml/swagger_petstore.yaml @@ -134,6 +134,7 @@ paths: items: type: string collectionFormat: multi + x-example: adorable responses: "200": description: successful operation @@ -174,6 +175,7 @@ paths: required: true type: integer format: int64 + x-example: 30 responses: "404": description: Pet not found