这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@
<module>xml</module>
<module>xstream</module>
<module>libraries-data-io-2</module>
<module>spring-swagger-codegen/openapi-custom-generator</module>
<module>spring-swagger-codegen/openapi-custom-generator-api-client</module>
</modules>

<properties>
Expand Down
136 changes: 136 additions & 0 deletions spring-swagger-codegen/openapi-custom-generator-api-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<artifactId>openapi-custom-generator-api-client</artifactId>
<name>openapi-custom-generator-api-client</name>
<description>OpenAPI custom generator sample</description>
<properties>
<java.version>17</java.version>
<openapi-generator.version>7.5.0</openapi-generator.version>
<camel.version>4.5.0</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
<version>${camel.version}</version>
</dependency>

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
<version>${camel.version}</version>
</dependency>

<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>${camel.version}</version>
<!-- use the same version as your Camel core version -->
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<configuration>
<skipValidateSpec>true</skipValidateSpec>
<inputSpec>${project.basedir}/src/main/resources/api/quotes.yaml</inputSpec>
</configuration>
<executions>
<execution>
<id>generate-camel-client</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>java-camel-client</generatorName>
<generateModels>false</generateModels>
<configOptions>
<dateLibrary>java8</dateLibrary>
<openApiNullable>false</openApiNullable>
<apiPackage>com.baeldung.tutorials.openapi.quotes.client</apiPackage>
<modelPackage>com.baeldung.tutorials.openapi.quotes.api.model</modelPackage>
</configOptions>
<globalProperties>
<!-- Change these flags to 'true' to enable build-time debug output -->
<debugOpenAPI>false</debugOpenAPI>
<debugModels>false</debugModels>
<debugOperations>false</debugOperations>
<debugSupportingFiles>false</debugSupportingFiles>
</globalProperties>
</configuration>
</execution>
<execution>
<id>generate-models</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>java</generatorName>
<generateApis>false</generateApis>
<generateModels>true</generateModels>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelTests>false</generateModelTests>
<generateApiTests>false</generateApiTests>
<configOptions>
<library>native</library>
<serializationLibrary>jackson</serializationLibrary>
<dateLibrary>java8</dateLibrary>
<openApiNullable>false</openApiNullable>
<useJakartaEe>true</useJakartaEe> <!-- SpringBoot 3.x uses JakartaEE -->
<apiPackage>com.baeldung.tutorials.openapi.quotes.api</apiPackage>
<modelPackage>com.baeldung.tutorials.openapi.quotes.api.model</modelPackage>
</configOptions>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>openapi-custom-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.tutorials.openapi.quotes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
openapi: '3.0.0'
info:
title: Quotes API
version: '1.0.0'
servers:
- description: Test server
url: http://localhost:8080
- description: Production Server
url: https://example.com/v1
paths:
/quotes/{symbol}:
get:
tags:
- quotes
summary: Get current quote for a security
operationId: getQuote
security:
- ApiKey:
- Quotes.Read
parameters:
- name: symbol
in: path
required: true
description: Security's symbol
schema:
type: string
pattern: '[A-Z0-9]+'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/QuoteResponse'
components:
securitySchemes:
ApiKey:
type: apiKey
in: header
name: X-API-KEY
schemas:
QuoteResponse:
description: Quote response
type: object
properties:
symbol:
type: string
description: security's symbol
price:
type: number
description: Quote value
timestamp:
type: string
format: date-time
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=openapi-custom-generator-api-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.tutorials.openapi.quotes;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.AdviceWithBuilder;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.baeldung.tutorials.openapi.quotes.client.QuotesApi;

@SpringBootTest
class ApplicationUnitTest {

@Autowired
private FluentProducerTemplate producer;

@Autowired
private CamelContext camel;

@Test
void whenInvokeGeneratedRoute_thenSuccess() throws Exception {
AdviceWith.adviceWith(camel, QuotesApi.GET_QUOTE_ROUTE_ID, in -> {
in.mockEndpointsAndSkip("rest:*");
});

Exchange exg = producer.to(QuotesApi.GET_QUOTE)
.withHeader("symbol", "BAEL")
.send();
assertNotNull(exg);
}
}
2 changes: 2 additions & 0 deletions spring-swagger-codegen/openapi-custom-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
target
85 changes: 85 additions & 0 deletions spring-swagger-codegen/openapi-custom-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# OpenAPI Generator for the java-camel-client library

## Overview
This is a boiler-plate project to generate your own project derived from an OpenAPI specification.
Its goal is to get you started with the basic plumbing so you can put in your own logic.
It won't work without your changes applied.

## What's OpenAPI
The goal of OpenAPI is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.
When properly described with OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.
Similar to what interfaces have done for lower-level programming, OpenAPI removes the guesswork in calling the service.

Check out [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) for additional information about the OpenAPI project, including additional libraries with support for other languages and more.

## How do I use this?
At this point, you've likely generated a client setup. It will include something along these lines:

```
.
|- README.md // this file
|- pom.xml // build script
|-- src
|--- main
|---- java
|----- com.baeldung.openapi.generators.camelclient.JavaCamelClientGenerator.java // generator file
|---- resources
|----- java-camel-client // template files
|----- META-INF
|------ services
|------- org.openapitools.codegen.CodegenConfig
```

You _will_ need to make changes in at least the following:

`JavaCamelClientGenerator.java`

Templates in this folder:

`src/main/resources/java-camel-client`

Once modified, you can run this:

```
mvn package
```

In your generator project. A single jar file will be produced in `target`. You can now use that with [OpenAPI Generator](https://openapi-generator.tech):

For mac/linux:
```
java -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g java-camel-client -i /path/to/openapi.yaml -o ./test
```
(Do not forget to replace the values `/path/to/openapi-generator-cli.jar`, `/path/to/your.jar` and `/path/to/openapi.yaml` in the previous command)

For Windows users, you will need to use `;` instead of `:` in the classpath, e.g.
```
java -cp /path/to/openapi-generator-cli.jar;/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g java-camel-client -i /path/to/openapi.yaml -o ./test
```

Now your templates are available to the client generator and you can write output values

## But how do I modify this?
The `JavaCamelClientGenerator.java` has comments in it--lots of comments. There is no good substitute
for reading the code more, though. See how the `JavaCamelClientGenerator` implements `CodegenConfig`.
That class has the signature of all values that can be overridden.

You can also step through JavaCamelClientGenerator.java in a debugger. Just debug the JUnit
test in DebugCodegenLauncher. That runs the command line tool and lets you inspect what the code is doing.

For the templates themselves, you have a number of values available to you for generation.
You can execute the `java` command from above while passing different debug flags to show
the object you have available during client generation:

```
# The following additional debug options are available for all codegen targets:
# -DdebugOpenAPI prints the OpenAPI Specification as interpreted by the codegen
# -DdebugModels prints models passed to the template engine
# -DdebugOperations prints operations passed to the template engine
# -DdebugSupportingFiles prints additional data passed to the template engine

java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g java-camel-client -i /path/to/openapi.yaml -o ./test
```

Will, for example, output the debug info for operations.
You can use this info in the `api.mustache` file.
6 changes: 6 additions & 0 deletions spring-swagger-codegen/openapi-custom-generator/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# This script creates new OpenAPI generator project.
mkdir -p target
wget -O target/openapi-generator-cli.jar https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.4.0/openapi-generator-cli-7.4.0.jar
java -jar target/openapi-generator-cli.jar meta \
-o . -n java-camel-client -p com.baeldung.openapi.generators.camelclient
40 changes: 40 additions & 0 deletions spring-swagger-codegen/openapi-custom-generator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-swagger-codegen</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>openapi-custom-generator</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator</artifactId>
<version>${openapi-generator-version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openapi-generator-version>7.5.0</openapi-generator-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.13.2</junit-version>
</properties>
</project>
Loading