-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix(online): thread-safe fileMap with TTL cleanup, SLF4J logging, and Content-Length header #24011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wing328
merged 7 commits into
OpenAPITools:master
from
vikas0686:f/vikas/openapi-generator/use-concurrent-hash-map
Jun 28, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92d6c23
fix : thread safety, logging, and Content-Length in openapi-generator…
vikas0686 f418c78
fix : thread-safe fileMap with 24h TTL cleanup in openapi-generator-o…
vikas0686 3aaaa56
fix(online): enforce TTL at download time and make createdAt immutable
vikas0686 67631d7
fix : updated the failing test cases for GenApiControllerTest
vikas0686 5720305
fix: Add GenApiServiceTest with TTL cleanup and concurrent generation…
vikas0686 709a677
fix(online): retain fileMap entry when temp directory deletion fails
vikas0686 53abb23
test(online): make GenApiServiceTest hermetic and leak-free
vikas0686 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...rator-online/src/test/java/org/openapitools/codegen/online/service/GenApiServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package org.openapitools.codegen.online.service; | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openapitools.codegen.online.model.Generated; | ||
| import org.openapitools.codegen.online.model.ResponseCode; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| import java.io.File; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| public class GenApiServiceTest { | ||
|
|
||
| private static final String OPENAPI_URL = localPetstoreUrl(); | ||
|
|
||
| private static String localPetstoreUrl() { | ||
| URL resource = GenApiServiceTest.class.getClassLoader().getResource("petstore.json"); | ||
| if (resource == null) { | ||
| throw new IllegalStateException("petstore.json not found in test resources"); | ||
| } | ||
| return resource.toExternalForm(); | ||
| } | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @Autowired | ||
| private GenApiService genApiService; | ||
|
|
||
| // Fix #1: TTL cleanup removes expired entries and deletes the temp directory | ||
| @Test | ||
| public void cleanExpiredFilesRemovesExpiredEntry() throws Exception { | ||
| File tempDir = Files.createTempDirectory("codegen-test").toFile(); | ||
| File bundle = new File(tempDir, "bundle.zip"); | ||
| bundle.createNewFile(); | ||
|
|
||
| Generated entry = new Generated(); | ||
| entry.setFilename(bundle.getAbsolutePath()); | ||
| entry.setFriendlyName("test"); | ||
| entry.setCreatedAt(Instant.now().minusSeconds(25 * 3600)); | ||
| genApiService.putFileEntry("test-expired-key", entry); | ||
|
|
||
| genApiService.cleanExpiredFiles(); | ||
|
|
||
| assertNull(genApiService.getFileEntry("test-expired-key"), "Expired entry should have been evicted"); | ||
| assertFalse(tempDir.exists(), "Temp directory should have been deleted"); | ||
| } | ||
|
|
||
| // Fix: entry is retained if directory deletion fails (no orphaned files) | ||
| @Test | ||
| public void cleanExpiredFilesRetainsEntryWhenDeletionFails() throws Exception { | ||
| // Point filename at a path whose "parent" is a regular file — deleteDirectory will fail | ||
| File fakeParent = Files.createTempFile("codegen-not-a-dir", ".tmp").toFile(); | ||
| try { | ||
| Generated entry = new Generated(); | ||
| entry.setFilename(new File(fakeParent, "bundle.zip").getAbsolutePath()); | ||
| entry.setFriendlyName("test"); | ||
| entry.setCreatedAt(Instant.now().minusSeconds(25 * 3600)); | ||
| genApiService.putFileEntry("test-deletion-fail-key", entry); | ||
|
|
||
| genApiService.cleanExpiredFiles(); | ||
|
|
||
| assertNotNull(genApiService.getFileEntry("test-deletion-fail-key"), "Entry should be retained when deletion fails"); | ||
| } finally { | ||
| fakeParent.delete(); | ||
| genApiService.removeFileEntry("test-deletion-fail-key"); | ||
| } | ||
| } | ||
|
|
||
| // Fix #1: recent entry is not evicted by cleanup | ||
| @Test | ||
| public void cleanExpiredFilesKeepsRecentEntry() throws Exception { | ||
| File tempDir = Files.createTempDirectory("codegen-test").toFile(); | ||
| try { | ||
| Generated entry = new Generated(); | ||
| entry.setFilename(new File(tempDir, "bundle.zip").getAbsolutePath()); | ||
| entry.setFriendlyName("test"); | ||
| genApiService.putFileEntry("test-recent-key", entry); | ||
|
|
||
| genApiService.cleanExpiredFiles(); | ||
|
|
||
| assertNotNull(genApiService.getFileEntry("test-recent-key"), "Recent entry should not be evicted"); | ||
| } finally { | ||
| genApiService.removeFileEntry("test-recent-key"); | ||
| tempDir.delete(); | ||
| } | ||
| } | ||
|
|
||
| // Fix #1: missing fileId returns 404 | ||
| @Test | ||
| public void downloadMissingFileReturns404() throws Exception { | ||
| mockMvc.perform(get("/api/gen/download/nonexistent-id")) | ||
| .andExpect(status().isNotFound()); | ||
| } | ||
|
|
||
| // Fix #1: existing entry past TTL returns 404 (TTL enforced at request time) | ||
| @Test | ||
| public void downloadExpiredEntryReturns404() throws Exception { | ||
| String result = mockMvc.perform(post("/api/gen/clients/java") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}")) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| .andExpect(status().isOk()) | ||
| .andReturn().getResponse().getContentAsString(); | ||
| String code = new ObjectMapper().readValue(result, ResponseCode.class).getCode(); | ||
|
|
||
| genApiService.getFileEntry(code).setCreatedAt(Instant.now().minusSeconds(25 * 3600)); | ||
|
|
||
| mockMvc.perform(get("/api/gen/download/" + code)) | ||
| .andExpect(status().isNotFound()); | ||
| } | ||
|
|
||
| // Fix #1: concurrent generation does not lose entries (thread-safety) | ||
| @Test | ||
| public void concurrentGenerationDoesNotLoseEntries() throws Exception { | ||
| int threads = 5; | ||
| CountDownLatch latch = new CountDownLatch(threads); | ||
| ExecutorService executor = Executors.newFixedThreadPool(threads); | ||
| List<String> codes = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < threads; i++) { | ||
| executor.submit(() -> { | ||
| try { | ||
| String result = mockMvc.perform(post("/api/gen/clients/java") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}")) | ||
| .andExpect(status().isOk()) | ||
| .andReturn().getResponse().getContentAsString(); | ||
| synchronized (codes) { | ||
| codes.add(new ObjectMapper().readValue(result, ResponseCode.class).getCode()); | ||
| } | ||
| } catch (Exception e) { | ||
| fail("Concurrent generation failed: " + e.getMessage()); | ||
| } finally { | ||
| latch.countDown(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| latch.await(); | ||
| executor.shutdown(); | ||
| assertEquals(threads, codes.size(), "All concurrent generations should succeed"); | ||
| assertEquals(threads, codes.stream().distinct().count(), "All codes should be unique"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.