Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed Jun 4, 2024
1 parent 997cfa8 commit b70afef
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.aether.repository.LocalRepository;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -225,6 +226,43 @@ public void testInputSpecRootDirectoryDoesNotRequireInputSpec() throws Exception
assertTrue(hashFolder.resolve("_merged_spec.yaml-executionId.sha256").toFile().exists());
}

/**
* Regression test for <a href="https://github.com/OpenAPITools/openapi-generator/issues/4512">#4512</a>
*/
public void test_skipIfSpecIsUnchanged_WillNotGenerateTwice_forMultipleExecutions_withIdenticalFilenames() throws Exception {

//GIVEN
/* Setup the mojo */
final Path tempDir = newTempFolder();
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/issue-4512", null, "execution_petstore1");
mojo.inputSpecRootDirectory = tempDir.toString();


/* Perform an initial generation */
mojo.execute();

/* Check the hash file was created */
final Path hashFolder = tempDir.resolve("target/generated-sources/issue-4512/.openapi-generator");
assertTrue(hashFolder.resolve("petstore.yaml-execution_petstore1.sha256").toFile().exists());

Check failure on line 246 in modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test results

org.openapitools.codegen.plugin.CodeGenMojoTest ► test_skipIfSpecIsUnchanged_WillNotGenerateTwice_forMultipleExecutions_withIdenticalFilenames

Failed test found in: modules/openapi-generator-maven-plugin/target/surefire-reports/TEST-org.openapitools.codegen.plugin.CodeGenMojoTest.xml Error: junit.framework.AssertionFailedError
Raw output
junit.framework.AssertionFailedError
	at junit.framework.Assert.fail(Assert.java:55)
	at junit.framework.Assert.assertTrue(Assert.java:22)
	at junit.framework.Assert.assertTrue(Assert.java:31)
	at junit.framework.TestCase.assertTrue(TestCase.java:200)
	at org.openapitools.codegen.plugin.CodeGenMojoTest.test_skipIfSpecIsUnchanged_WillNotGenerateTwice_forMultipleExecutions_withIdenticalFilenames(CodeGenMojoTest.java:246)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at junit.framework.TestCase.runTest(TestCase.java:177)
	at junit.framework.TestCase.runBare(TestCase.java:142)
	at junit.framework.TestResult$1.protect(TestResult.java:122)
	at junit.framework.TestResult.runProtected(TestResult.java:142)
	at junit.framework.TestResult.run(TestResult.java:125)
	at junit.framework.TestCase.run(TestCase.java:130)
	at junit.framework.TestSuite.runTest(TestSuite.java:241)
	at junit.framework.TestSuite.run(TestSuite.java:236)
assertTrue(hashFolder.resolve("petstore.yaml-execution_petstore2.sha256").toFile().exists());

/* Remove the generated source */
FileUtils.deleteDirectory(tempDir.resolve("target/generated-sources/issue-4512/src").toFile());


// WHEN
/* Execute the mojo again */
mojo.execute();

// THEN
/* Verify that the source directory has not been repopulated. If it has then we generated code again */
assertFalse(
"src directory should not have been regenerated",
tempDir.resolve("target/generated-sources/issue-4512/src").toFile().exists()
);

}

protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String profile) throws Exception {
return loadMojo(temporaryFolder, projectRoot, profile, "default");
}
Expand All @@ -236,6 +274,8 @@ protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String
MojoExecution execution = newMojoExecution("generate");
MojoExecution executionWithId = copyWithExecutionId(executionId, execution);
return (CodeGenMojo) lookupConfiguredMojo(session, executionWithId);
// codegenMojo.inputSpecRootDirectory = temporaryFolder.toString();
// return codegenMojo;
}

private MojoExecution copyWithExecutionId(String executionId, MojoExecution execution) {
Expand All @@ -262,9 +302,13 @@ protected MavenProject readMavenProject(Path basedir, String profile) throws Exc
return project;
}

@SneakyThrows
static private Path newTempFolder() {
var tempDir = Files.createTempDirectory("test");
final Path tempDir;
try {
tempDir = Files.createTempDirectory("test");
} catch (IOException e) {
throw new RuntimeException(e);
}
tempDir.toFile().deleteOnExit();
return tempDir;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: This is a sample Petstore **ONE** server.
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: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: 'http://petstore.swagger.io/api/oauth/dialog'
scopes:
'write:pets': modify pets in your account
'read:pets': read your pets
api_key:
type: apiKey
name: api_key
in: header
schemas:
Pet:
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
@@ -0,0 +1,146 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: This is a sample Petstore **TWO** server.
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: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: 'http://petstore.swagger.io/api/oauth/dialog'
scopes:
'write:pets': modify pets in your account
'read:pets': read your pets
api_key:
type: apiKey
name: api_key
in: header
schemas:
Pet:
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
Loading

0 comments on commit b70afef

Please sign in to comment.