forked from loafle/openapi-generator-original
[JAVA] - fix BUG 14233 code gen support multiple accept headers, fallback is json/application (#15245)
This commit is contained in:
parent
62459f4a9a
commit
e384201416
@ -1736,7 +1736,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
String contentType = consumes.isEmpty() ? defaultContentType : consumes.get(0);
|
String contentType = consumes.isEmpty() ? defaultContentType : consumes.get(0);
|
||||||
operation.addExtension("x-content-type", contentType);
|
operation.addExtension("x-content-type", contentType);
|
||||||
}
|
}
|
||||||
String accepts = getAccept(openAPI, operation);
|
String accepts = getAccepts(openAPI, operation);
|
||||||
operation.addExtension("x-accepts", accepts);
|
operation.addExtension("x-accepts", accepts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1787,32 +1787,19 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getAccept(OpenAPI openAPI, Operation operation) {
|
private static String getAccepts(OpenAPI openAPIArg, Operation operation) {
|
||||||
String accepts = null;
|
final Set<String> producesInfo = getProducesInfo(openAPIArg, operation);
|
||||||
String defaultContentType = "application/json";
|
|
||||||
Set<String> producesInfo = getProducesInfo(openAPI, operation);
|
|
||||||
if (producesInfo != null && !producesInfo.isEmpty()) {
|
if (producesInfo != null && !producesInfo.isEmpty()) {
|
||||||
ArrayList<String> produces = new ArrayList<>(producesInfo);
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (String produce : produces) {
|
for (String produce : producesInfo) {
|
||||||
if (defaultContentType.equalsIgnoreCase(produce)) {
|
if (sb.length() > 0) {
|
||||||
accepts = defaultContentType;
|
sb.append(",");
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
if (sb.length() > 0) {
|
|
||||||
sb.append(",");
|
|
||||||
}
|
|
||||||
sb.append(produce);
|
|
||||||
}
|
}
|
||||||
|
sb.append(produce);
|
||||||
}
|
}
|
||||||
if (accepts == null) {
|
return sb.toString();
|
||||||
accepts = sb.toString();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
accepts = defaultContentType;
|
|
||||||
}
|
}
|
||||||
|
return "application/json"; // default media type
|
||||||
return accepts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -73,14 +73,13 @@ public class AbstractJavaCodegenTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPreprocessOpenAPI() throws Exception {
|
public void testPreprocessOpenApiIncludeAllMediaTypesInAcceptHeader() throws Exception {
|
||||||
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
|
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
|
||||||
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
|
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
|
||||||
|
|
||||||
codegen.preprocessOpenAPI(openAPI);
|
codegen.preprocessOpenAPI(openAPI);
|
||||||
|
|
||||||
Assert.assertEquals(codegen.getArtifactVersion(), openAPI.getInfo().getVersion());
|
Assert.assertEquals(codegen.getArtifactVersion(), openAPI.getInfo().getVersion());
|
||||||
Assert.assertEquals(openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts"), "application/json");
|
Assert.assertEquals(openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts"), "application/json,application/xml");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user