Skip to content
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
2 changes: 2 additions & 0 deletions openapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies {
compileOnly "io.micronaut:micronaut-inject-java"

implementation "io.micronaut:micronaut-http"
implementation "io.micronaut:micronaut-http-server"

api "io.swagger.core.v3:swagger-core:$swaggerVersion"
api "io.swagger.core.v3:swagger-models:$swaggerVersion"
api "io.swagger.core.v3:swagger-annotations:$swaggerVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.MediaType;
import io.micronaut.http.server.types.files.FileCustomizableResponseType;
import io.micronaut.http.uri.UriMatchTemplate;
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.ast.Element;
Expand Down Expand Up @@ -79,6 +80,7 @@
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.reactivestreams.Publisher;
Expand All @@ -97,9 +99,12 @@
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -690,6 +695,9 @@ private boolean isTypeNullable(ClassElement type) {
if (schema != null) {
schema = arraySchema(schema);
}
} else if (isReturnTypeFile(type)) {
schema = new StringSchema();
schema.setFormat("binary");
} else {
schema = getSchemaDefinition(openAPI, context, type, definingElement, mediaTypes);
}
Expand Down Expand Up @@ -1451,6 +1459,15 @@ private boolean isContainerType(ClassElement type) {
).stream().anyMatch(type::isAssignable);
}

private boolean isReturnTypeFile(ClassElement type) {
return CollectionUtils.setOf(
FileCustomizableResponseType.class.getName(),
File.class.getName(),
InputStream.class.getName(),
ByteBuffer.class.getName()
).stream().anyMatch(type::isAssignable);
}

/**
* Processes {@link io.swagger.v3.oas.annotations.security.SecurityScheme}
* annotations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.micronaut.openapi.visitor

import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation

class OpenApiFileResponseTypeSpec extends AbstractTypeElementSpec {
def setup() {
System.setProperty(AbstractOpenApiVisitor.ATTR_TEST_MODE, "true")
}

void "test build the OpenAPI for returning files"() {
when:
buildBeanDefinition('test.MyBean', '''
package test;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.*;
import io.micronaut.http.server.types.files.*;

import java.io.*;
import java.nio.*;
import java.util.UUID;

@Controller("/upload")
class FooController {

@Produces("application/pdf")
@Get("/response-streamed-file/{documentId}")
public HttpResponse<StreamedFile> action1(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/response-system-file/{documentId}")
public HttpResponse<SystemFile> action2(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/streamed-file/{documentId}")
public StreamedFile action3(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/system-file/{documentId}")
public SystemFile action4(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/file/{documentId}")
public File action5(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/input-stream/{documentId}")
public InputStream action6(UUID documentId) {
return null;
}

@Produces("application/pdf")
@Get("/byte-buffer/{documentId}")
public ByteBuffer action7(UUID documentId) {
return null;
}
}

@jakarta.inject.Singleton
class MyBean {}
''')

OpenAPI openAPI = AbstractOpenApiVisitor.testReference

then:
openAPI
openAPI.paths.size() == 7
openAPI.paths.each {
assert it.value.get.responses.size() == 1
assert it.value.get.responses['200'].content['application/pdf'].schema.type == 'string'
assert it.value.get.responses['200'].content['application/pdf'].schema.format == 'binary'
assert it.value.get.responses['200'].content['application/pdf'].schema.$ref == null
}
}

void "test build the OpenAPI for returning files and check their annotations takes precedence"() {
when:
buildBeanDefinition('test.MyBean', '''
package test;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.*;
import io.micronaut.http.server.types.files.*;
import io.swagger.v3.oas.annotations.responses.*;
import io.swagger.v3.oas.annotations.media.*;

import java.util.UUID;

@Controller("/upload")
class FooController {

@Produces("application/octet-stream")
@Get("/{documentId}")
@ApiResponse(responseCode = "200", content = @Content(mediaType = "application/octet-stream", schema = @Schema(type = "string", format = "binary")))
public HttpResponse<StreamedFile> action(UUID documentId) {
return null;
}
}

@jakarta.inject.Singleton
class MyBean {}
''')

OpenAPI openAPI = AbstractOpenApiVisitor.testReference

then: 'The content is the one defined in the @Content annotation'
openAPI
openAPI.paths.size() == 1
Operation operation = openAPI.paths.get('/upload/{documentId}').get
operation.responses['200'].content['application/octet-stream'].schema.type == 'string'
operation.responses['200'].content['application/octet-stream'].schema.format == 'binary'
operation.responses['200'].content['application/octet-stream'].schema.$ref == null
}
}