Search in sources :

Example 1 with CodegenOperation

use of io.swagger.codegen.v3.CodegenOperation in project hippo by NHS-digital-website.

the class ApiSpecificationStaticHtml2Codegen method sortedAccordingToCustomOrder.

private List<CodegenOperation> sortedAccordingToCustomOrder(List<CodegenOperation> operations, String groupName) {
    final Predicate matchesGroupName = operationOrderEntry -> Optional.ofNullable(((LinkedHashMap<?, ?>) operationOrderEntry).get("group")).map(String.class::cast).map(this::toApiVarName).orElse("default").equals(groupName);
    LinkedHashMap<String, Object> operationOrderGroup = (LinkedHashMap<String, Object>) Optional.ofNullable(openAPI.getExtensions()).map(extensions -> extensions.get("x-spec-publication")).map(LinkedHashMap.class::cast).map(xSpecPublication -> xSpecPublication.get("operation-order")).map(List.class::cast).orElse(emptyList()).stream().map(LinkedHashMap.class::cast).filter(matchesGroupName).findFirst().orElse(new LinkedHashMap<>());
    if (operationOrderGroup.isEmpty()) {
        return operations;
    }
    List<LinkedHashMap> ops = (List<LinkedHashMap>) Optional.ofNullable(operationOrderGroup.get("operations")).orElse(new ArrayList<>());
    Function<CodegenOperation, Integer> getReferenceIndexOf = op -> {
        LinkedHashMap<?, ?> matchingReference = ops.stream().filter(opReference -> ((String) opReference.get("method")).equalsIgnoreCase(op.getHttpMethod()) && opReference.get("path").equals(op.getPath())).findFirst().orElse(new LinkedHashMap());
        return ops.indexOf(matchingReference);
    };
    operations.sort(Comparator.comparingInt(getReferenceIndexOf::apply));
    return operations;
}
Also used : java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Operation(io.swagger.v3.oas.models.Operation) Header(io.swagger.v3.oas.models.headers.Header) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) OpenAPI(io.swagger.v3.oas.models.OpenAPI) uk.nhs.digital.common.components.apispecification.handlebars(uk.nhs.digital.common.components.apispecification.handlebars) CodegenParameterExampleHtmlRenderer(uk.nhs.digital.common.components.apispecification.swagger.request.examplerenderer.CodegenParameterExampleHtmlRenderer) Handlebars(com.github.jknack.handlebars.Handlebars) ConditionalHelpers(com.github.jknack.handlebars.helper.ConditionalHelpers) StringHelpers(com.github.jknack.handlebars.helper.StringHelpers) AssignHelper(com.github.jknack.handlebars.helper.AssignHelper) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SchemaHelper(uk.nhs.digital.common.components.apispecification.handlebars.schema.SchemaHelper) Collections.emptyMap(java.util.Collections.emptyMap) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) io.swagger.codegen.v3(io.swagger.codegen.v3) io.swagger.v3.oas.models.media(io.swagger.v3.oas.models.media) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) BodyWithMediaTypesExtractor(uk.nhs.digital.common.components.apispecification.swagger.model.BodyWithMediaTypesExtractor) Info(io.swagger.v3.oas.models.info.Info) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Stream(java.util.stream.Stream) CommonmarkMarkdownConverter(uk.nhs.digital.common.components.apispecification.commonmark.CommonmarkMarkdownConverter) TypeAnySanitisingHelper(uk.nhs.digital.common.components.apispecification.handlebars.schema.TypeAnySanitisingHelper) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Components(io.swagger.v3.oas.models.Components) StaticHtml2Codegen(io.swagger.codegen.v3.generators.html.StaticHtml2Codegen) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate)

Example 2 with CodegenOperation

use of io.swagger.codegen.v3.CodegenOperation in project flow by vaadin.

the class CodeGenerator method setShouldShowTsDoc.

private void setShouldShowTsDoc(List<CodegenOperation> operations) {
    for (CodegenOperation coop : operations) {
        boolean hasDescription = GeneratorUtils.isNotBlank(coop.getNotes());
        boolean hasParameter = hasParameterDescription(coop);
        boolean hasResponseDescription = hasResponseDescription(coop);
        if (hasDescription || hasParameter || hasResponseDescription) {
            coop.getVendorExtensions().put(EXTENSION_VAADIN_CONNECT_SHOW_TSDOC, true);
        }
    }
}
Also used : CodegenOperation(io.swagger.codegen.v3.CodegenOperation)

Example 3 with CodegenOperation

use of io.swagger.codegen.v3.CodegenOperation in project flow by vaadin.

the class CodeGenerator method postProcessOperations.

@Override
@SuppressWarnings("unchecked")
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
    Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
    String classname = (String) operations.get("classname");
    for (Tag tag : tags) {
        if (tag.getName().equals(classname)) {
            objs.put(VAADIN_CONNECT_CLASS_DESCRIPTION, tag.getDescription());
            setVaadinFilePath(objs, tag);
            break;
        }
    }
    if (objs.get(VAADIN_CONNECT_CLASS_DESCRIPTION) == null) {
        logger.debug("The class '{}' doesn't have JavaDoc or it is invalid. This results in no TsDoc for the generated module '{}'.", classname, classname);
    }
    if ((operations.get(OPERATION) instanceof List)) {
        List<CodegenOperation> codegenOperations = (List<CodegenOperation>) operations.get(OPERATION);
        setShouldShowTsDoc(codegenOperations);
    }
    Map<String, Object> postProcessOperations = super.postProcessOperations(objs);
    List<Map<String, Object>> imports = (List<Map<String, Object>>) objs.get("imports");
    adjustImportInformationForEndpoints(imports);
    printDebugMessage(postProcessOperations, "=== All operations data ===");
    return postProcessOperations;
}
Also used : CodegenOperation(io.swagger.codegen.v3.CodegenOperation) ArrayList(java.util.ArrayList) List(java.util.List) Tag(io.swagger.v3.oas.models.tags.Tag) Map(java.util.Map)

Example 4 with CodegenOperation

use of io.swagger.codegen.v3.CodegenOperation in project hippo by NHS-digital-website.

the class CodegenOperationSorter method getOperationsToAdd.

private List<CodegenOperation> getOperationsToAdd(List<CodegenOperation> flattenedOpList, List<Map<String, String>> operations) {
    final List<CodegenOperation> operationsToAdd = new ArrayList<>();
    for (Map<String, String> opReference : operations) {
        String path = opReference.get("path");
        String httpMethod = opReference.get("method");
        CodegenOperation operation = flattenedOpList.stream().filter(op -> op.getPath().equals(path) && op.getHttpMethod().equals(httpMethod)).findAny().orElse(null);
        if (operation == null) {
            throw new RuntimeException("Could not find operation that matches reference: " + httpMethod + " " + path);
        }
        operationsToAdd.add(operation);
    }
    return operationsToAdd;
}
Also used : CodegenOperation(io.swagger.codegen.v3.CodegenOperation)

Example 5 with CodegenOperation

use of io.swagger.codegen.v3.CodegenOperation in project hippo by NHS-digital-website.

the class CodegenOperationSorterTest method operationWithPathAndMethod.

private static CodegenOperation operationWithPathAndMethod(String path, String httpMethod) {
    CodegenOperation op = new CodegenOperation();
    op.path = path;
    op.httpMethod = httpMethod;
    return op;
}
Also used : CodegenOperation(io.swagger.codegen.v3.CodegenOperation)

Aggregations

CodegenOperation (io.swagger.codegen.v3.CodegenOperation)5 Handlebars (com.github.jknack.handlebars.Handlebars)1 AssignHelper (com.github.jknack.handlebars.helper.AssignHelper)1 ConditionalHelpers (com.github.jknack.handlebars.helper.ConditionalHelpers)1 StringHelpers (com.github.jknack.handlebars.helper.StringHelpers)1 io.swagger.codegen.v3 (io.swagger.codegen.v3)1 StaticHtml2Codegen (io.swagger.codegen.v3.generators.html.StaticHtml2Codegen)1 Components (io.swagger.v3.oas.models.Components)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Operation (io.swagger.v3.oas.models.Operation)1 Header (io.swagger.v3.oas.models.headers.Header)1 Info (io.swagger.v3.oas.models.info.Info)1 io.swagger.v3.oas.models.media (io.swagger.v3.oas.models.media)1 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)1 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)1 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)1 Tag (io.swagger.v3.oas.models.tags.Tag)1 LocalDate (java.time.LocalDate)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1