cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Cloud SDK OpenAPI Client with CAP?

MioYasutake
Active Contributor
0 Kudos

Hi community,

I'm attempting to use the Cloud SDK's OpenAPI Client in a CAP Java project, but I'm unable to access the generated API client. The structure of my project is as follows:

MioYasuatke_0-1708665010936.png

The OpenAPI Client has been generated with the following setting in pom.xml.

			<plugin>
				<groupId>com.sap.cloud.sdk.datamodel</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>${cloud.sdk.version}</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<phase>generate-sources</phase>
					</execution>
				</executions>
				<configuration>
					<inputSpec>${project.basedir}/api-specs/AuthorizationAPI.yaml</inputSpec>
					<outputDirectory>${project.basedir}/openapi</outputDirectory>
					<apiPackage>customer.cap_openapi.generated.authorization.api</apiPackage>
					<modelPackage>customer.cap_openapi.generated.authorization.model</modelPackage>
					<apiMaturity>released</apiMaturity>
				</configuration>
			</plugin>	

Here's the source for the CAP event handler:

package customer.cap_openapi.handlers;

import org.springframework.stereotype.Component;

import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;

import customer.cap_openapi.generated.authorization.api.RoleCollectionsApi;
import cds.gen.authorizationservice.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@ServiceName(AuthorizationService_.CDS_NAME)
class AuthorizationServiceHandler {

    Logger logger = LoggerFactory.getLogger(AuthorizationServiceHandler.class);
    
    @On(event = GetRoleCollectionsContext.CDS_NAME)
    public void getRoleCollections(GetRoleCollectionsContext context) {
        logger.info("getRoleCollection handler");

        HttpDestination destination = DestinationAccessor.getDestination("xsuaa").asHttp();
        // RoleCollectionsApi rolecollectionApi = new RoleCollectionsApi(destination);
        
    }
}

I'm encountering the error "The import customer.cap_openapi.generated cannot be resolved". Is the srv/openapi directory the appropriate place to generate the OpenAPI Client? How can I make the Open API client accessible from the event handler source?

The code for the project is available in the following Git repository: https://github.com/miyasuta/cap-openapi/tree/generate_client

View Entire Topic
MioYasutake
Active Contributor

The issue has been resolved with the following output directory:

<outputDirectory>${project.basedir}/src/gen/java</outputDirectory>