Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update openapi version to 2.0 #16

Merged
merged 1 commit into from
Oct 26, 2021
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
38 changes: 36 additions & 2 deletions launcher-impl/microprofile/openapi/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Fujitsu Limited and/or its affiliates. All rights reserved.
Copyright (c) 2019-2021 Fujitsu Limited and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -30,7 +30,36 @@
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api-1.1</artifactId>
<artifactId>smallrye-open-api-core</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api-jaxrs</artifactId>
</dependency>
<!-- Unit Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -47,6 +76,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Fujitsu Limited and/or its affiliates. All rights reserved.
* Copyright (c) 2019-2021 Fujitsu Limited and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,6 +17,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import javax.inject.Inject;

Expand All @@ -39,7 +40,7 @@
import io.smallrye.openapi.api.OpenApiDocument;
import io.smallrye.openapi.runtime.OpenApiProcessor;
import io.smallrye.openapi.runtime.OpenApiStaticFile;
import io.smallrye.openapi.runtime.io.OpenApiSerializer.Format;
import io.smallrye.openapi.runtime.io.Format;
/**
*
* @author Katsuhiro Kunisada
Expand Down Expand Up @@ -166,10 +167,10 @@ private boolean isJarToBeScanned(OpenApiConfig config, String entry) {
}

private boolean isClassToBeScanned(OpenApiConfig config, String entry) {
Set<String> scanClasses = config.scanClasses();
Set<String> scanPackages = config.scanPackages();
Set<String> scanExcludeClasses = config.scanExcludeClasses();
Set<String> scanExcludePackages = config.scanExcludePackages();
Pattern scanClasses = config.scanClasses();
Pattern scanPackages = config.scanPackages();
Pattern scanExcludeClasses = config.scanExcludeClasses();
Pattern scanExcludePackages = config.scanExcludePackages();

if (entry == null) {
return false;
Expand All @@ -179,16 +180,16 @@ private boolean isClassToBeScanned(OpenApiConfig config, String entry) {
String packageName = getPackageName(fqcn);
boolean ret;

if (scanClasses.isEmpty() && scanPackages.isEmpty()) {
if (scanClasses.pattern().isEmpty() && scanPackages.pattern().isEmpty()) {
ret = true;
} else if (!scanClasses.isEmpty() && scanPackages.isEmpty()) {
ret = scanClasses.contains(fqcn);
} else if (scanClasses.isEmpty() && !scanPackages.isEmpty()) {
ret = scanPackages.contains(packageName);
} else if (!scanClasses.pattern().isEmpty() && scanPackages.pattern().isEmpty()) {
ret = scanClasses.matcher(fqcn).matches();
} else if (scanClasses.pattern().isEmpty() && !scanPackages.pattern().isEmpty()) {
ret = scanPackages.matcher(packageName).matches();
} else {
ret = scanClasses.contains(fqcn) || scanPackages.contains(packageName);
ret = scanClasses.matcher(fqcn).matches() || scanPackages.matcher(packageName).matches();
}
if (scanExcludeClasses.contains(fqcn) || scanExcludePackages.contains(packageName)) {
if (scanExcludeClasses.matcher(fqcn).matches() || scanExcludePackages.matcher(packageName).matches()) {
ret = false;
}
return ret;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Fujitsu Limited and/or its affiliates. All rights reserved.
* Copyright (c) 2019-2021 Fujitsu Limited and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -10,52 +10,91 @@
package com.fujitsu.launcher.microprofile.openapi;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.smallrye.openapi.api.OpenApiDocument;
import io.smallrye.openapi.runtime.io.Format;
import io.smallrye.openapi.runtime.io.OpenApiSerializer;
import io.smallrye.openapi.runtime.io.OpenApiSerializer.Format;

import org.glassfish.jersey.message.internal.AcceptableMediaType;
import org.glassfish.jersey.message.internal.HttpHeaderReader;

import java.text.ParseException;

import javax.ws.rs.core.MediaType;

/**
*
* @author Takahiro Nagao
* @author Katsuhiro Kunisada
* @author Koki Kosaka
*/
@SuppressWarnings("serial")
@WebServlet
public class OpenApiServlet extends HttpServlet {
public static final String MEDIA_TYPE_YAML = "text/plain";
public static final String MEDIA_TYPE_JSON = "application/json";
public static final Map<Format, String> ACCEPTED_TYPES = new HashMap<>();

static {
ACCEPTED_TYPES.put(Format.YAML, MediaType.TEXT_PLAIN);
ACCEPTED_TYPES.put(Format.JSON, MediaType.APPLICATION_JSON);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Format format = Format.YAML;
String type = MEDIA_TYPE_YAML;
Format format = getResponseFormat(request);
if (format != null) {
String oai = OpenApiSerializer.serialize(OpenApiDocument.INSTANCE.get(), format);
response.setContentType(ACCEPTED_TYPES.get(format));
response.getWriter().write(oai);
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
}
}

if (isJson(request)) {
format = Format.JSON;
type = MEDIA_TYPE_JSON;
protected Format getResponseFormat(HttpServletRequest request) {
try {
Format format = parseFormatQueryParameter(request);
if (format != null) return format;
return parseAcceptHeader(request);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

String oai = OpenApiSerializer.serialize(OpenApiDocument.INSTANCE.get(), format);
response.getWriter().write(oai);
response.setContentType(type);
response.setStatus(200);
private Format parseFormatQueryParameter(HttpServletRequest request) {
String formatParam = request.getParameter("format");
if (formatParam != null) {
for (Format f : Format.values()) {
if (f.name().equalsIgnoreCase(formatParam)) return f;
}
}
return null;
}

private boolean isJson(HttpServletRequest request) {
String accept = request.getHeader("Accept");
if (accept != null && accept.equals(MEDIA_TYPE_JSON)) {
return true;
private Format parseAcceptHeader(HttpServletRequest request) throws ParseException {
String acceptHeader = request.getHeader("Accept");
// sorted by quality value
List<AcceptableMediaType> mediaTypes = HttpHeaderReader.readAcceptMediaType(acceptHeader);
if (mediaTypes.isEmpty()) {
mediaTypes.add(AcceptableMediaType.valueOf(MediaType.WILDCARD_TYPE));
}
String formatParam = request.getParameter("format");
if (formatParam != null && formatParam.equals("json")) {
return true;

for (AcceptableMediaType mediaType : mediaTypes) {
if (mediaType.isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
return Format.YAML;
}
if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
return Format.JSON;
}
}
return false;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2021 Fujitsu Limited and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/

package com.fujitsu.launcher.microprofile.openapi;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.smallrye.openapi.runtime.io.Format;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class OpenApiServletTest {

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpServletResponse mockResponse = mock(HttpServletResponse.class);

OpenApiServlet servlet = spy(new OpenApiServlet());

@ParameterizedTest
@MethodSource({"provideOnlyAcceptHeader", "provideOnlyFormatQuery", "provideFormatQueryAndAcceptHeader"})
public void testGetResponseFormat(String format, String acceptHeader, Format expected) {
when(mockRequest.getParameter("format")).thenReturn(format);
when(mockRequest.getHeader("Accept")).thenReturn(acceptHeader);
Format result = servlet.getResponseFormat(mockRequest);
assertEquals(result, expected);
}

private static Stream<Arguments> provideOnlyAcceptHeader() {
return Stream.of(
// one value
Arguments.of(null, "", Format.YAML),
Arguments.of(null, "text/plain", Format.YAML),
Arguments.of(null, "application/json", Format.JSON),
Arguments.of(null, "*/*", Format.YAML),
Arguments.of(null, "text/*", Format.YAML),
Arguments.of(null, "application/*", Format.JSON),
Arguments.of(null, "hoge/hoge", null),

// two value
Arguments.of(null, "text/plain, application/json", Format.YAML),
Arguments.of(null, "application/json, text/plain", Format.JSON),
Arguments.of(null, "text/plain;q=0.9, application/json", Format.JSON),
Arguments.of(null, "application/json;q=0.9, text/plain", Format.YAML),

Arguments.of(null, "*/*, application/json", Format.JSON),
Arguments.of(null, "text/*, application/json", Format.JSON),
Arguments.of(null, "application/*, text/plain", Format.YAML),
Arguments.of(null, "text/*, application/json;q=0.9", Format.YAML),
Arguments.of(null, "application/*, text/plain;q=0.9", Format.JSON),

// three value
Arguments.of(null, "*/*, application/*, text/plain", Format.YAML),
Arguments.of(null, "*/*, application/*, text/plain;q=0.9", Format.JSON),
Arguments.of(null, "*/*, application/*;q=0.9, text/plain;q=0.8", Format.YAML)
);
}

private static Stream<Arguments> provideOnlyFormatQuery() {
return Stream.of(
// one value
Arguments.of("YAML", null, Format.YAML),
Arguments.of("JSON", null, Format.JSON),
Arguments.of("yaml", null, Format.YAML),
Arguments.of("json", null, Format.JSON),
Arguments.of("hoge", null, Format.YAML),
Arguments.of(null, null, Format.YAML)
);
}

private static Stream<Arguments> provideFormatQueryAndAcceptHeader() {
return Stream.of(
Arguments.of("YAML", "application/json", Format.YAML),
Arguments.of("JSON", "text/plain", Format.JSON),
Arguments.of("hoge", "application/json", Format.JSON)
);
}

@Test
public void testSendError406() throws IOException {
doReturn(null).when(servlet).getResponseFormat(mockRequest);
servlet.doGet(mockRequest, mockResponse);
verify(mockResponse, times(1)).sendError(406);
}

@Test
public void testThrowRuntimeException() {
when(mockRequest.getParameter("format")).thenReturn(null);
when(mockRequest.getHeader("Accept")).thenReturn("application/json:q=0.4"); //colon
assertThrows(RuntimeException.class, () -> servlet.getResponseFormat(mockRequest));
}

}
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<mp.health.version>3.1</mp.health.version>
<mp.jwt-auth.version>1.2.2</mp.jwt-auth.version>
<mp.metrics.version>3.0</mp.metrics.version>
<mp.openapi.version>1.1.2</mp.openapi.version>
<mp.openapi.version>2.0</mp.openapi.version>
<mp.opentracing.version>1.3.1</mp.opentracing.version>
<mp.rest-client.version>1.3.4</mp.rest-client.version>
<glassfish.version>5.1.0</glassfish.version>
Expand All @@ -44,7 +44,7 @@
<smallrye.health.version>3.1.1</smallrye.health.version>
<smallrye.jwt.version>3.3.0</smallrye.jwt.version>
<smallrye.metrics.version>3.0.3</smallrye.metrics.version>
<smallrye.openapi.version>1.0.2</smallrye.openapi.version>
<smallrye.openapi.version>2.1.15</smallrye.openapi.version>
<smallrye.opentracing.version>1.3.2</smallrye.opentracing.version>
<args4j.version>2.33</args4j.version>
<jackson.version>2.9.4</jackson.version>
Expand Down Expand Up @@ -285,7 +285,12 @@
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api-1.1</artifactId>
<artifactId>smallrye-open-api-core</artifactId>
<version>${smallrye.openapi.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-open-api-jaxrs</artifactId>
<version>${smallrye.openapi.version}</version>
</dependency>
<dependency>
Expand Down