Skip to content
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

Fix divide operations by content type #1762

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1181,37 +1181,53 @@ private List<Operation> divideOperationsByContentType(String path, PathItem.Http
if (content.size() <= 1) {
return Collections.emptyList();
}
var firstEntry = content.entrySet().iterator().next();
var mediaTypesToRemove = new ArrayList<String>();
for (var entry : content.entrySet()) {
if (mediaTypesToRemove.contains(entry.getKey())) {
if (mediaTypesToRemove.contains(entry.getKey()) || entry.getKey().equals(firstEntry.getKey()) || entry.getValue().equals(firstEntry.getValue())) {
continue;
}
for (var entry2 : content.entrySet()) {
if (entry.getKey().equals(entry2.getKey()) || entry.getValue().equals(entry2.getValue())) {
continue;
var foundSameOpSignature = false;
for (var additionalOp : additionalOps) {
RequestBody additionalBody = additionalOp.getRequestBody();
if (additionalBody == null || additionalBody.getContent() == null) {
return Collections.emptyList();
}
for (var addContentEntry : additionalBody.getContent().entrySet()) {
if (addContentEntry.getValue().equals(entry.getValue())) {
foundSameOpSignature = true;
break;
}
}
if (foundSameOpSignature) {
additionalBody.getContent().put(entry.getKey(), entry.getValue());
break;
}
additionalOps.add(new Operation()
.deprecated(op.getDeprecated())
.callbacks(op.getCallbacks())
.description(op.getDescription())
.extensions(op.getExtensions())
.externalDocs(op.getExternalDocs())
.operationId(getOrGenerateOperationId(op, path, httpMethod.name()))
.parameters(op.getParameters())
.responses(op.getResponses())
.security(op.getSecurity())
.servers(op.getServers())
.summary(op.getSummary())
.tags(op.getTags())
.requestBody(new RequestBody()
.description(body.getDescription())
.extensions(body.getExtensions())
.content(new Content()
.addMediaType(entry2.getKey(), entry2.getValue()))
)
);
mediaTypesToRemove.add(entry2.getKey());
}
mediaTypesToRemove.add(entry.getKey());
if (foundSameOpSignature) {
continue;
}
additionalOps.add(new Operation()
.deprecated(op.getDeprecated())
.callbacks(op.getCallbacks())
.description(op.getDescription())
.extensions(op.getExtensions())
.externalDocs(op.getExternalDocs())
.operationId(getOrGenerateOperationId(op, path, httpMethod.name()))
.parameters(op.getParameters())
.responses(op.getResponses())
.security(op.getSecurity())
.servers(op.getServers())
.summary(op.getSummary())
.tags(op.getTags())
.requestBody(new RequestBody()
.description(body.getDescription())
.extensions(body.getExtensions())
.content(new Content()
.addMediaType(entry.getKey(), entry.getValue()))
)
);
}
if (!mediaTypesToRemove.isEmpty()) {
content.entrySet().removeIf(stringMediaTypeEntry -> mediaTypesToRemove.contains(stringMediaTypeEntry.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ void testMultipleContentTypesEndpoints() {

assertFileContains(path + "api/DefaultApi.java", """
@Post("/multiplecontentpath")
@Produces({"application/json", "application/xml"})
Mono<HttpResponse<Void>> myOp(
@Body @Nullable @Valid Coordinates coordinates
);
Expand All @@ -782,6 +783,13 @@ Mono<HttpResponse<Void>> myOp_1(
@Nullable @Valid Coordinates coordinates,
@Nullable byte[] file
);
""",
"""
@Post("/multiplecontentpath")
@Produces({"application/yaml", "text/json"})
Mono<HttpResponse<Void>> myOp_2(
@Body @Nullable @Valid MySchema mySchema
);
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,28 +537,29 @@ void testMultipleContentTypesEndpoints() {
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/DefaultApi.java", """
@Operation(
operationId = "myOp",
responses = @ApiResponse(responseCode = "201", description = "Successfully created")
)
@Post("/multiplecontentpath")
@Consumes({"application/json", "application/xml"})
@Secured(SecurityRule.IS_ANONYMOUS)
Mono<HttpResponse<Void>> myOp(
@Body @Nullable(inherited = true) @Valid Coordinates coordinates
);
""",
"""
@Operation(
operationId = "myOp_0",
responses = @ApiResponse(responseCode = "201", description = "Successfully created")
)
@Post("/multiplecontentpath")
@Consumes("multipart/form-data")
@Secured(SecurityRule.IS_ANONYMOUS)
Mono<HttpResponse<Void>> myOp_1(
@Nullable(inherited = true) @Valid Coordinates coordinates,
@Nullable(inherited = true) CompletedFileUpload file
);
""",
"""
@Post("/multiplecontentpath")
@Consumes({"application/yaml", "text/json"})
@Secured(SecurityRule.IS_ANONYMOUS)
Mono<HttpResponse<Void>> myOp_2(
@Body @Nullable(inherited = true) @Valid MySchema mySchema
);
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ void testMultipleContentTypesEndpoints() {

assertFileContains(path + "api/DefaultApi.kt", """
@Post("/multiplecontentpath")
@Produces("application/json", "application/xml")
fun myOp(
@Body @Nullable @Valid coordinates: Coordinates?
): Mono<HttpResponse<Void>>
Expand All @@ -807,6 +808,13 @@ fun myOp_1(
@Nullable @Valid coordinates: Coordinates?,
@Nullable file: ByteArray?
): Mono<HttpResponse<Void>>
""",
"""
@Post("/multiplecontentpath")
@Produces("application/yaml", "text/json")
fun myOp_2(
@Body @Nullable @Valid mySchema: MySchema?
): Mono<HttpResponse<Void>>
""");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.micronaut.openapi.generator;

import io.micronaut.openapi.generator.assertions.TestUtils;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;
Expand Down Expand Up @@ -622,32 +621,29 @@ void testMultipleContentTypesEndpoints() {
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "api/DefaultApi.kt", """
@Operation(
operationId = "myOp",
responses = [
ApiResponse(responseCode = "201", description = "Successfully created")
]
)
@Post("/multiplecontentpath")
@Consumes("application/json", "application/xml")
@Secured(SecurityRule.IS_ANONYMOUS)
fun myOp(
@Body @Nullable @Valid coordinates: Coordinates?
): Mono<HttpResponse<Void>>
""",
"""
@Operation(
operationId = "myOp_0",
responses = [
ApiResponse(responseCode = "201", description = "Successfully created")
]
)
@Post("/multiplecontentpath")
@Consumes("multipart/form-data")
@Secured(SecurityRule.IS_ANONYMOUS)
fun myOp_1(
@Nullable @Valid coordinates: Coordinates?,
@Nullable file: CompletedFileUpload?
): Mono<HttpResponse<Void>>
""",
"""
@Post("/multiplecontentpath")
@Consumes("application/yaml", "text/json")
@Secured(SecurityRule.IS_ANONYMOUS)
fun myOp_2(
@Body @Nullable @Valid mySchema: MySchema?
): Mono<HttpResponse<Void>>
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/coordinates'
application/xml:
schema:
$ref: '#/components/schemas/coordinates'
multipart/form-data:
schema:
type: object
Expand All @@ -20,6 +23,12 @@ paths:
file:
type: string
format: binary
application/yaml:
schema:
$ref: '#/components/schemas/MySchema'
text/json:
schema:
$ref: '#/components/schemas/MySchema'
responses:
201:
description: Successfully created
Expand All @@ -39,3 +48,10 @@ components:
type: number
long:
type: number
MySchema:
type: object
required:
- lat
properties:
lat:
type: number
Loading