Skip to content

Commit

Permalink
[Java RESTEasy client] added filename directive for Java RESTEasy fil…
Browse files Browse the repository at this point in the history
…e uploads (OpenAPITools#17367) (OpenAPITools#17435)

* added filename directive for Java RESTEasy file uploads (OpenAPITools#17367)

* regenerated samples
  • Loading branch information
miladhub authored Dec 21, 2023
1 parent bf4c98a commit 1060479
Show file tree
Hide file tree
Showing 212 changed files with 26,114 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bin/configs/java-resteasy-echo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: java
outputDir: samples/client/petstore/java/resteasy-echo
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-echo.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
artifactId: petstore-resteasy-echo
hideGenerationTimestamp: "true"
library: resteasy
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
try {
multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE);
multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE, file.getName());
} catch (FileNotFoundException e) {
throw new ApiException("Could not serialize multipart/form-data "+e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import junit.framework.AssertionFailedError;
import lombok.SneakyThrows;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
Expand Down Expand Up @@ -764,6 +766,50 @@ public void testAuthorizationScopeValues_Issue6733() throws IOException {
files.forEach(File::deleteOnExit);
}

@Test
public void testMultiPartSpecifiesFileName_Issue17367() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.RESTEASY)
.setValidateSpec(false)
.setInputSpec("src/test/resources/3_0/issue-17367.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");
generator.setGenerateMetadata(false);
List<File> files = generator.opts(clientOptInput).generate();
try {
validateJavaSourceFiles(files);
File apiClient = files.stream()
.filter(f -> f.getName().equals("ApiClient.java"))
.findFirst()
.orElseThrow(() -> new AssertionFailedError(
"ApiClient.java not found"));

Stream<String> contents = Arrays.stream(Files.readString(apiClient.toPath(),
StandardCharsets.UTF_8).split("\n"));

// https://docs.jboss.org/resteasy/docs/6.2.5.Final/javadocs/org/jboss/resteasy/plugins/providers/multipart/MultipartFormDataOutput.html#addFormData(java.lang.String,java.lang.Object,jakarta.ws.rs.core.MediaType,java.lang.String)
assertTrue(contents.anyMatch(l -> l.matches(
".*multipart\\.addFormData\\(param.getKey\\(\\),\\s*" +
"new\\s+FileInputStream\\(file\\),\\s*" +
"MediaType\\.APPLICATION_OCTET_STREAM_TYPE,\\s*" +
"file.getName\\(\\)\\);.*")));
} finally {
files.forEach(File::deleteOnExit);
}
}

@Test
public void testAuthorizationsMethodsSizeWhenFiltered() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue4584.yaml");
Expand Down
24 changes: 24 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue-17367.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Foo API
servers:
- url: http://127.0.0.1:8080/{basePath}
variables:
basePath:
default: v1
paths:
/foo:
post:
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
bar:
type: string
format: binary
responses:
'200':
description: OK
Loading

0 comments on commit 1060479

Please sign in to comment.