public PathsDocument(Swagger2MarkupConverter.Context context) { super(context); this.pathOperationComponent = new PathOperationComponent(context, new DefinitionDocumentResolverFromOperation(context), new SecurityDocumentResolver(context)); this.operationDocumentNameResolver = new OperationDocumentNameResolver(context); this.operationDocumentResolverDefault = new OperationDocumentResolverDefault(context); if (logger.isDebugEnabled()) { if (config.isGeneratedExamplesEnabled()) { logger.debug("Generate examples is enabled."); } else { logger.debug("Generate examples is disabled."); } if (config.isSeparatedOperationsEnabled()) { logger.debug("Create separated operation files is enabled."); } else { logger.debug("Create separated operation files is disabled."); } } }
/** * Builds a path operation. * * @param markupDocBuilder the docbuilder do use for output * @param operation the Swagger Operation */ private void applyPathOperationComponent(MarkupDocBuilder markupDocBuilder, PathOperation operation) { if (operation != null) { pathOperationComponent.apply(markupDocBuilder, PathOperationComponent.parameters(operation)); } }
/** * Builds inline schema definitions * * @param markupDocBuilder the docbuilder do use for output * @param definitions all inline definitions to display * @param uniquePrefix unique prefix to prepend to inline object names to enforce unicity */ private void inlineDefinitions(MarkupDocBuilder markupDocBuilder, List<ObjectType> definitions, String uniquePrefix) { if (CollectionUtils.isNotEmpty(definitions)) { for (ObjectType definition : definitions) { addInlineDefinitionTitle(markupDocBuilder, definition.getName(), definition.getUniqueName()); List<ObjectType> localDefinitions = new ArrayList<>(); propertiesTableComponent.apply(markupDocBuilder, PropertiesTableComponent.parameters( definition.getProperties(), uniquePrefix, localDefinitions )); for (ObjectType localDefinition : localDefinitions) inlineDefinitions(markupDocBuilder, Collections.singletonList(localDefinition), localDefinition.getUniqueName()); } } }
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEFORE, markupDocBuilder, operation)); buildOperationTitle(markupDocBuilder, operation); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEGIN, markupDocBuilder, operation)); buildDeprecatedSection(markupDocBuilder, operation); buildDescriptionSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_DESCRIPTION)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildParametersSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_PARAMETERS)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildBodyParameterSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); inlineDefinitions(markupDocBuilder, buildResponsesSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_RESPONSES)) markupDocBuilder.pageBreak(); buildConsumesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_CONSUMES)) markupDocBuilder.pageBreak(); buildProducesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_PRODUCES)) markupDocBuilder.pageBreak(); buildTagsSection(markupDocBuilder, operation); buildSecuritySchemeSection(markupDocBuilder, operation); buildExamplesSection(markupDocBuilder, operation, locations); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_END, markupDocBuilder, operation)); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_AFTER, markupDocBuilder, operation));
@Test public void testPathOperationComponent() throws URISyntaxException { String COMPONENT_NAME = "path_operation"; Path outputDirectory = getOutputFile(COMPONENT_NAME); FileUtils.deleteQuietly(outputDirectory.toFile()); //Given Path file = Paths.get(PathOperationComponentTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).build(); Swagger swagger = converter.getContext().getSwagger(); io.swagger.models.Path path = swagger.getPaths().get("/pets"); List<PathOperation> pathOperations = PathUtils.toPathOperationsList("/pets", 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)); }
private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> exampleMap, String operationSectionTitle, String sectionTitle, boolean beforeBreak, boolean afterBreak) { if (exampleMap.size() > 0) { if (beforeBreak) markupDocBuilder.pageBreak(); buildSectionTitle(markupDocBuilder, operationSectionTitle); for (Map.Entry<String, Object> entry : exampleMap.entrySet()) { buildExampleTitle(markupDocBuilder, sectionTitle + " " + entry.getKey()); JsonNode rootNode = parseExample(entry.getValue()); Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields(); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = stripExampleQuotes(field.getValue().toString()); example = StringEscapeUtils.unescapeJava(example); if (entryByType.getKey().equals("application/json")) { String example = Json.pretty(entryByType.getValue()); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = stripExampleQuotes(entryByType.getValue().toString()); example = StringEscapeUtils.unescapeJava(example);
/** * Adds a operation description to the document. * * @param operation the Swagger Operation */ private void buildDescriptionSection(MarkupDocBuilder markupDocBuilder, PathOperation operation) { MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEGIN, descriptionBuilder, operation)); String description = operation.getOperation().getDescription(); if (isNotBlank(description)) { descriptionBuilder.paragraph(markupDescription(config.getSwaggerMarkupLanguage(), markupDocBuilder, description)); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_END, descriptionBuilder, operation)); String descriptionContent = descriptionBuilder.toString(); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEFORE, markupDocBuilder, operation)); if (isNotBlank(descriptionContent)) { buildSectionTitle(markupDocBuilder, labels.getLabel(DESCRIPTION)); markupDocBuilder.text(descriptionContent); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_AFTER, markupDocBuilder, operation)); }
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEFORE, markupDocBuilder, operation)); buildOperationTitle(markupDocBuilder, operation); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEGIN, markupDocBuilder, operation)); buildDeprecatedSection(markupDocBuilder, operation); buildDescriptionSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_DESCRIPTION)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildParametersSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_PARAMETERS)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildBodyParameterSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); inlineDefinitions(markupDocBuilder, buildResponsesSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_RESPONSES)) markupDocBuilder.pageBreak(); buildConsumesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_CONSUMES)) markupDocBuilder.pageBreak(); buildProducesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_PRODUCES)) markupDocBuilder.pageBreak(); buildTagsSection(markupDocBuilder, operation); buildSecuritySchemeSection(markupDocBuilder, operation); buildExamplesSection(markupDocBuilder, operation, locations); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_END, markupDocBuilder, operation)); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_AFTER, markupDocBuilder, operation));
@Test public void testInlineSchema() throws URISyntaxException { String COMPONENT_NAME = "path_operation_inline_schema"; Path outputDirectory = getOutputFile(COMPONENT_NAME); FileUtils.deleteQuietly(outputDirectory.toFile()); //Given Path file = Paths.get(PathOperationComponentTest.class.getResource("/yaml/swagger_inlineSchema.yaml").toURI()); Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).build(); Swagger swagger = converter.getContext().getSwagger(); io.swagger.models.Path path = swagger.getPaths().get("/LaunchCommand"); List<PathOperation> pathOperations = PathUtils.toPathOperationsList("/LaunchCommand", 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)); }
private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> exampleMap, String operationSectionTitle, String sectionTitle, boolean beforeBreak, boolean afterBreak) { if (exampleMap.size() > 0) { if (beforeBreak) markupDocBuilder.pageBreak(); buildSectionTitle(markupDocBuilder, operationSectionTitle); for (Map.Entry<String, Object> entry : exampleMap.entrySet()) { buildExampleTitle(markupDocBuilder, sectionTitle + " " + entry.getKey()); JsonNode rootNode = parseExample(entry.getValue()); Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields(); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = stripExampleQuotes(field.getValue().toString()); example = StringEscapeUtils.unescapeJava(example);
/** * Adds a operation description to the document. * * @param operation the Swagger Operation */ private void buildDescriptionSection(MarkupDocBuilder markupDocBuilder, PathOperation operation) { MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEGIN, descriptionBuilder, operation)); String description = operation.getOperation().getDescription(); if (isNotBlank(description)) { descriptionBuilder.paragraph(markupDescription(config.getSwaggerMarkupLanguage(), markupDocBuilder, description)); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_END, descriptionBuilder, operation)); String descriptionContent = descriptionBuilder.toString(); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEFORE, markupDocBuilder, operation)); if (isNotBlank(descriptionContent)) { buildSectionTitle(markupDocBuilder, labels.getLabel(DESCRIPTION)); markupDocBuilder.text(descriptionContent); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_AFTER, markupDocBuilder, operation)); }
applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEFORE, markupDocBuilder, operation)); buildOperationTitle(markupDocBuilder, operation); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_BEGIN, markupDocBuilder, operation)); buildDeprecatedSection(markupDocBuilder, operation); buildDescriptionSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_DESCRIPTION)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildParametersSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_PARAMETERS)) markupDocBuilder.pageBreak(); inlineDefinitions(markupDocBuilder, buildBodyParameterSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); inlineDefinitions(markupDocBuilder, buildResponsesSection(markupDocBuilder, operation), operation.getPath() + " " + operation.getMethod()); if (locations.contains(AFTER_OPERATION_RESPONSES)) markupDocBuilder.pageBreak(); buildConsumesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_CONSUMES)) markupDocBuilder.pageBreak(); buildProducesSection(markupDocBuilder, operation); if (locations.contains(AFTER_OPERATION_PRODUCES)) markupDocBuilder.pageBreak(); buildTagsSection(markupDocBuilder, operation); buildSecuritySchemeSection(markupDocBuilder, operation); buildExamplesSection(markupDocBuilder, operation, locations); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_END, markupDocBuilder, operation)); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_AFTER, markupDocBuilder, operation));
@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<String, String> 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<PathOperation> 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)); }
private void exampleMap(MarkupDocBuilder markupDocBuilder, Map<String, Object> exampleMap, String operationSectionTitle, String sectionTitle, boolean beforeBreak, boolean afterBreak) { if (exampleMap.size() > 0) { if (beforeBreak) markupDocBuilder.pageBreak(); buildSectionTitle(markupDocBuilder, operationSectionTitle); for (Map.Entry<String, Object> entry : exampleMap.entrySet()) { buildExampleTitle(markupDocBuilder, sectionTitle + " " + entry.getKey()); JsonNode rootNode = parseExample(entry.getValue()); Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields(); example = stripExampleQuotes(StringEscapeUtils.unescapeJson(example)); String example = stripExampleQuotes(field.getValue().toString()); example = StringEscapeUtils.unescapeJava(example);
/** * Builds a path operation. * * @param markupDocBuilder the docbuilder do use for output * @param operation the Swagger Operation */ private void applyPathOperationComponent(MarkupDocBuilder markupDocBuilder, PathOperation operation) { if (operation != null) { pathOperationComponent.apply(markupDocBuilder, PathOperationComponent.parameters(operation)); } }
/** * Adds a operation description to the document. * * @param operation the Swagger Operation */ private void buildDescriptionSection(MarkupDocBuilder markupDocBuilder, PathOperation operation) { MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEGIN, descriptionBuilder, operation)); String description = operation.getOperation().getDescription(); if (isNotBlank(description)) { descriptionBuilder.paragraph(markupDescription(config.getSwaggerMarkupLanguage(), markupDocBuilder, description)); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_END, descriptionBuilder, operation)); String descriptionContent = descriptionBuilder.toString(); applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_BEFORE, markupDocBuilder, operation)); if (isNotBlank(descriptionContent)) { buildSectionTitle(markupDocBuilder, labels.getLabel(DESCRIPTION)); markupDocBuilder.text(descriptionContent); } applyPathsDocumentExtension(new PathsDocumentExtension.Context(Position.OPERATION_DESCRIPTION_AFTER, markupDocBuilder, operation)); }
/** * Builds inline schema definitions * * @param markupDocBuilder the docbuilder do use for output * @param definitions all inline definitions to display * @param uniquePrefix unique prefix to prepend to inline object names to enforce unicity */ private void inlineDefinitions(MarkupDocBuilder markupDocBuilder, List<ObjectType> definitions, String uniquePrefix) { if (CollectionUtils.isNotEmpty(definitions)) { for (ObjectType definition : definitions) { addInlineDefinitionTitle(markupDocBuilder, definition.getName(), definition.getUniqueName()); List<ObjectType> localDefinitions = new ArrayList<>(); propertiesTableComponent.apply(markupDocBuilder, PropertiesTableComponent.parameters( definition.getProperties(), uniquePrefix, localDefinitions )); for (ObjectType localDefinition : localDefinitions) inlineDefinitions(markupDocBuilder, Collections.singletonList(localDefinition), localDefinition.getUniqueName()); } } }
public PathsDocument(Swagger2MarkupConverter.Context context) { super(context); this.pathOperationComponent = new PathOperationComponent(context, new DefinitionDocumentResolverFromOperation(context), new SecurityDocumentResolver(context)); this.operationDocumentNameResolver = new OperationDocumentNameResolver(context); this.operationDocumentResolverDefault = new OperationDocumentResolverDefault(context); if (logger.isDebugEnabled()) { if (config.isGeneratedExamplesEnabled()) { logger.debug("Generate examples is enabled."); } else { logger.debug("Generate examples is disabled."); } if (config.isSeparatedOperationsEnabled()) { logger.debug("Create separated operation files is enabled."); } else { logger.debug("Create separated operation files is disabled."); } } }
@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<String, String> 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<PathOperation> 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)); }
/** * Builds a path operation. * * @param markupDocBuilder the docbuilder do use for output * @param operation the Swagger Operation */ private void applyPathOperationComponent(MarkupDocBuilder markupDocBuilder, PathOperation operation) { if (operation != null) { pathOperationComponent.apply(markupDocBuilder, PathOperationComponent.parameters(operation)); } }