Skip to content

Commit

Permalink
Provide better compatibility for projects migrating from OAS 3.0 to O…
Browse files Browse the repository at this point in the history
…AS 3.1. Fixes #2849
  • Loading branch information
bnasslahsen committed Jan 12, 2025
1 parent 88f5da0 commit 911fb5f
Show file tree
Hide file tree
Showing 565 changed files with 42,605 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2025 the original author or authors.
* * * * * *
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * * you may not use this file except in compliance with the License.
* * * * * * You may obtain a copy of the License at
* * * * * *
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * * *
* * * * * * Unless required by applicable law or agreed to in writing, software
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * * See the License for the specific language governing permissions and
* * * * * * limitations under the License.
* * * * *
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v31;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;
import org.springdoc.core.utils.Constants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.hamcrest.Matchers.is;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* The type Abstract spring doc test.
*/
@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureMockMvc
public abstract class AbstractSpringDocTest {

/**
* The constant className.
*/
public static String className;

/**
* The Mock mvc.
*/
@Autowired
protected MockMvc mockMvc;

/**
* Gets content.
*
* @param fileName the file name
* @return the content
* @throws Exception the exception
*/
public static String getContent(String fileName) throws Exception {
try {
Path path = Paths.get(AbstractSpringDocTest.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
}
catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
}
}

/**
* Test app.
*
* @throws Exception the exception
*/
@Test
protected void testApp() throws Exception {
className = getClass().getSimpleName();
String testNumber = className.replaceAll("[^0-9]", "");
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
.andExpect(jsonPath("$.openapi", is("3.1.0"))).andReturn();
String result = mockMvcResult.getResponse().getContentAsString();
String expected = getContent("results/3.1.0/app" + testNumber + ".json");
assertEquals(expected, result, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2025 the original author or authors.
* * * * * *
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * * you may not use this file except in compliance with the License.
* * * * * * You may obtain a copy of the License at
* * * * * *
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * * *
* * * * * * Unless required by applicable law or agreed to in writing, software
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * * See the License for the specific language governing permissions and
* * * * * * limitations under the License.
* * * * *
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v31.app1;

/**
* The type Api exception.
*/
public final class ApiException extends Exception {

/**
* The constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;

/**
* The Code.
*/
@SuppressWarnings("unused")

private final int code;

/**
* Instantiates a new Api exception.
*
* @param code the code
* @param msg the msg
*/
public ApiException(int code, String msg) {
super(msg);
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2025 the original author or authors.
* * * * * *
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * * you may not use this file except in compliance with the License.
* * * * * * You may obtain a copy of the License at
* * * * * *
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * * *
* * * * * * Unless required by applicable law or agreed to in writing, software
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * * See the License for the specific language governing permissions and
* * * * * * limitations under the License.
* * * * *
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v31.app1;

import java.io.IOException;

import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;

/**
* The type Api origin filter.
*/
class ApiOriginFilter implements jakarta.servlet.Filter {
/**
* Do filter.
*
* @param request the request
* @param response the response
* @param chain the chain
* @throws IOException the io exception
* @throws ServletException the servlet exception
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);
}

/**
* Destroy.
*/
@Override
public void destroy() {
}

/**
* Init.
*
* @param filterConfig the filter config
* @throws ServletException the servlet exception
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
Loading

0 comments on commit 911fb5f

Please sign in to comment.