Search in sources :

Example 1 with OperationDescription

use of io.syndesis.server.api.generator.openapi.util.OperationDescription in project syndesis by syndesisio.

the class OpenApiConnectorGenerator method configureConnector.

final Connector configureConnector(final ConnectorTemplate connectorTemplate, final Connector connector, final ConnectorSettings connectorSettings) {
    final Connector.Builder builder = new Connector.Builder().createFrom(connector);
    final OpenApiModelInfo info = parseSpecification(connectorSettings, APIValidationContext.NONE);
    final OasDocument openApiDoc = info.getModel();
    addGlobalParameters(builder, info);
    final OasPaths paths = ofNullable(openApiDoc.paths).orElse(openApiDoc.createPaths());
    final String connectorId = connector.getId().orElseThrow(() -> new IllegalArgumentException("Missing connector identifier"));
    final List<ConnectorAction> actions = new ArrayList<>();
    final Map<String, Integer> operationIdCounts = new HashMap<>();
    for (final OasPathItem path : OasModelHelper.getPathItems(paths)) {
        final Map<String, OasOperation> operationMap = OasModelHelper.getOperationMap(path);
        for (final Map.Entry<String, OasOperation> entry : operationMap.entrySet()) {
            final OasOperation operation = entry.getValue();
            final String operationId = operation.operationId;
            if (operationId == null) {
                operation.operationId = operationIdGenerator.get();
            } else {
                // we tolerate that some operations might have the same
                // operationId, if that's the case we generate a unique
                // operationId by appending the count of the duplicates,
                // e.g. operation ids for non unique operation id "foo" will
                // be "foo", "foo1", "foo2", ... this will be reflected in
                // the Swagger specification stored in `specification`
                // property
                final Integer count = operationIdCounts.compute(operationId, (id, currentCount) -> ofNullable(currentCount).map(c -> ++c).orElse(0));
                if (count > 0) {
                    operation.operationId = operationId + count;
                }
            }
            final ConnectorDescriptor descriptor = createDescriptor(connectorId, info, operation);
            final OperationDescription description = OasModelHelper.operationDescriptionOf(openApiDoc, operation, (m, p) -> "Send " + m + " request to " + p);
            final ConnectorAction action = new ConnectorAction.Builder().id(createActionId(connectorId, operation)).name(description.name).description(description.description).pattern(Action.Pattern.To).descriptor(descriptor).tags(OasModelHelper.sanitizeTags(operation.tags).distinct()::iterator).build();
            actions.add(action);
        }
    }
    actions.sort(ActionComparator.INSTANCE);
    builder.addAllActions(actions);
    builder.putConfiguredProperty(SPECIFICATION, SpecificationOptimizer.minimizeForComponent(openApiDoc));
    return builder.build();
}
Also used : OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) Connector(io.syndesis.common.model.connection.Connector) OasOperation(io.apicurio.datamodels.openapi.models.OasOperation) HashMap(java.util.HashMap) OasPaths(io.apicurio.datamodels.openapi.models.OasPaths) ArrayList(java.util.ArrayList) OperationDescription(io.syndesis.server.api.generator.openapi.util.OperationDescription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

OasDocument (io.apicurio.datamodels.openapi.models.OasDocument)1 OasOperation (io.apicurio.datamodels.openapi.models.OasOperation)1 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)1 OasPaths (io.apicurio.datamodels.openapi.models.OasPaths)1 ConnectorAction (io.syndesis.common.model.action.ConnectorAction)1 ConnectorDescriptor (io.syndesis.common.model.action.ConnectorDescriptor)1 Connector (io.syndesis.common.model.connection.Connector)1 OperationDescription (io.syndesis.server.api.generator.openapi.util.OperationDescription)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1