Skip to content

Commit

Permalink
Remove dependency scan for inputSpec (actually not required) (#19084)
Browse files Browse the repository at this point in the history
* added test with external-ref ("external dependency" installed before actual usage)

* fixed test with resource on classpath

added test with resource on classpath with external-ref.

Test with profile 'resource' was wrong as it took JAR into account, but there was no 'petstore.yaml' on classpath.

* Remove dependency scan and do not modify inputSpec provided by user

It is still possible to provide JARs as inputSpec, however the user has to provide it by hand.

Providing dependencies in plugin section still allows correct inputSpec resolution on classpath.
  • Loading branch information
parenko committed Jul 9, 2024
1 parent 039f0b8 commit 5612852
Show file tree
Hide file tree
Showing 14 changed files with 938 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/maven-plugin-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
run: |
./mvnw clean install -DskipTests -Dmaven.javadoc.skip=true
./mvnw --no-snapshot-updates --quiet clean compile -f modules/openapi-generator-maven-plugin/examples/java-client.xml -Dorg.slf4j.simpleLogger.defaultLogLevel=error
./mvnw --no-snapshot-updates --quiet clean install -f modules/openapi-generator-maven-plugin/examples/multi-module/sample-external-ref-schema/pom.xml -Dorg.slf4j.simpleLogger.defaultLogLevel=error
./mvnw --no-snapshot-updates --quiet clean compile -f modules/openapi-generator-maven-plugin/examples/multi-module/pom.xml -Dorg.slf4j.simpleLogger.defaultLogLevel=error
./mvnw --no-snapshot-updates --quiet clean compile -f modules/openapi-generator-maven-plugin/examples/kotlin.xml -Dorg.slf4j.simpleLogger.defaultLogLevel=error
./mvnw --no-snapshot-updates --quiet clean compile -f modules/openapi-generator-maven-plugin/examples/spring.xml -Dorg.slf4j.simpleLogger.defaultLogLevel=error
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@
<artifactId>sample-schema</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sample-external-ref-schema</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>default</id>
<goals>
<goal>generate</goal>
</goals>
Expand All @@ -48,6 +54,30 @@
<dateLibrary>joda</dateLibrary>
</configOptions>

<!-- override the default library to jersey2 -->
<library>jersey2</library>
</configuration>
</execution>
<execution>
<id>external-ref</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- specify the OpenAPI spec -->
<inputSpec>external-ref/petstore.yaml</inputSpec>

<!-- target to generate java client code -->
<generatorName>java</generatorName>

<!-- hint: if you want to generate java server code, e.g. based on Spring Boot,
you can use the following target: <generatorName>spring</generatorName> -->

<!-- pass any necessary config options -->
<configOptions>
<dateLibrary>joda</dateLibrary>
</configOptions>

<!-- override the default library to jersey2 -->
<library>jersey2</library>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<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>
<groupId>org.openapitools</groupId>
<artifactId>sample-external-ref-schema</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample-external-ref-schema</name>
<url>https://maven.apache.org</url>

<!-- this POM is not a module of multi-module project to keep resources out of classpath-->

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: Sample file with just two endpoints and one schema (defined in an external file $ref)
version: 1.0.0
title: OpenAPI Petstore
paths:
/pet:
post:
tags:
- pet
summary: Add a new pet to the store
description: ''
operationId: addPet
responses:
'200':
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
'405':
description: Invalid input
security:
- petstore_auth:
- 'write:pets'
- 'read:pets'
requestBody:
$ref: '#/components/requestBodies/Pet'
'/pet/{petId}':
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
'200':
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid ID supplied
'404':
description: Pet not found
security:
- api_key: []
delete:
tags:
- pet
summary: Deletes a pet
description: ''
operationId: deletePet
parameters:
- name: api_key
in: header
required: false
schema:
type: string
- name: petId
in: path
description: Pet id to delete
required: true
schema:
type: integer
format: int64
responses:
'400':
description: Invalid pet value
security:
- petstore_auth:
- 'write:pets'
- 'read:pets'
components:
requestBodies:
Pet:
content:
application/json:
schema:
$ref: 'schemas/Pet.yaml'
application/xml:
schema:
$ref: 'schemas/Pet.yaml'
description: Pet object that needs to be added to the store
required: true
schemas:
Pet:
$ref: 'schemas/Pet.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
title: a Pet
description: A pet for sale in the pet store
type: object
required:
- name
- photoUrls
properties:
id:
type: integer
format: int64
name:
type: string
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: Pet
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
* Goal which generates client/server code from a OpenAPI json/yaml definition.
*/
@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
public class CodeGenMojo extends AbstractMojo {

private final Logger LOGGER = LoggerFactory.getLogger(CodeGenMojo.class);
Expand Down Expand Up @@ -623,13 +623,7 @@ public void execute() throws MojoExecutionException {
}

if (isNotEmpty(inputSpec)) {
URL url = inputSpecRemoteUrl();

if ((! inputSpecFile.exists()) && url != null) {
configurator.setInputSpec(url.toString());
} else {
configurator.setInputSpec(inputSpec);
}
configurator.setInputSpec(inputSpec);
}

if (isNotEmpty(gitHost)) {
Expand Down Expand Up @@ -1015,35 +1009,15 @@ private String calculateInputSpecHash(String inputSpec) {
}

/**
* Try to parse inputSpec setting string into URL (truly remote or resource)
* Try to parse inputSpec setting string into URL
* @return A valid URL or null if inputSpec is not a valid URL
*/
private URL inputSpecRemoteUrl() {
URL url = dependencyClassLoader().getResource(inputSpec);

if (url == null) {
try {
url = new URI(FilenameUtils.separatorsToUnix(inputSpec)).toURL();
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
}
}

return url;
}

private ClassLoader dependencyClassLoader() {
List<URL> list = new ArrayList<>();

for (Artifact artifact : project.getArtifacts()) {
try {
if (artifact.isResolved() && artifact.getType().equals("jar")) {
list.add(new URL("jar:" + artifact.getFile().toURI() + "!/"));
}
} catch (Exception e) {
}
try {
return new URI(FilenameUtils.separatorsToUnix(inputSpec)).toURL();
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException ignored) {
return null;
}

return new URLClassLoader(list.toArray(new URL[] { }), getClass().getClassLoader());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public void testCommonConfigurationWithResourceInputSpec() throws Exception {
testCommonConfiguration("resource");
}

public void testCommonConfigurationWithURLInputSpec() throws Exception {
testCommonConfiguration("url");
public void testCommonConfigurationWithResourceExternalRefInputSpec() throws Exception {
testCommonConfiguration("resource-external-ref");
}

public void testCommonConfigurationWithJARInputSpec() throws Exception {
testCommonConfiguration("jar");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,30 @@
<profile>
<id>resource</id>
<properties>
<inputSpec>petstore.yaml</inputSpec>
<inputSpec>petstore-on-classpath.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>url</id>
<id>resource-external-ref</id>
<properties>
<!-- Directory schemas inside schema-external-ref to not shadow JARs schema directory -->
<inputSpec>schema-external-ref/petstore-on-classpath.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>jar</id>
<properties>
<inputSpec>jar:file:${basedir}/local-repo/petstore/schema/1/schema-1.jar!/petstore.yaml</inputSpec>
</properties>
<dependencies>
<dependency>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
</dependency>
</dependencies>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<finalName>common-maven</finalName>
<plugins>
Expand Down
Loading

0 comments on commit 5612852

Please sign in to comment.