forked from alibaba/fastjson2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e77e1ed
commit 7038d34
Showing
3 changed files
with
80 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
extension/src/main/java/com/alibaba/fastjson2/support/springdoc/OpenApiJsonWriter.java
This file was deleted.
Oops, something went wrong.
85 changes: 70 additions & 15 deletions
85
extension/src/test/java/com/alibaba/fastjson2/springdoc/OpenApiJsonWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,82 @@ | ||
package com.alibaba.fastjson2.springdoc; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONFactory; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.support.springdoc.OpenApiJsonWriter; | ||
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.util.List; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@WebAppConfiguration | ||
@ContextConfiguration | ||
public class OpenApiJsonWriterTest { | ||
String jsonStr = "{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"http://localhost:8099\",\"description\":\"Generated server url\"}],\"paths\":{},\"components\":{}}"; | ||
@Autowired | ||
private WebApplicationContext wac; | ||
|
||
@Test | ||
public void test() { | ||
OpenApiJsonWriter writer = OpenApiJsonWriter.INSTANCE; | ||
writer.write(JSONWriter.of(), null); | ||
writer.write(JSONWriter.of(), jsonStr); | ||
private MockMvc mockMvc; | ||
|
||
private static String openapi = "{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"http://localhost:8099\",\"description\":\"Generated server url\"}],\"paths\":{},\"components\":{}}"; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) // | ||
.addFilter(new CharacterEncodingFilter("UTF-8", true)) // 设置服务器端返回的字符集为:UTF-8 | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
JSONFactory.getDefaultObjectWriterProvider().register(String.class, OpenApiJsonWriter.INSTANCE); | ||
assertEquals(jsonStr, JSON.toJSONString(jsonStr)); | ||
JSONFactory.getDefaultObjectWriterProvider().unregister(String.class, OpenApiJsonWriter.INSTANCE); | ||
public void test() throws Exception { | ||
mockMvc.perform( | ||
(post("/test").characterEncoding("UTF-8") | ||
.contentType(MediaType.APPLICATION_JSON_VALUE) | ||
.content(openapi) | ||
)).andExpect(status().isOk()).andDo(print()); | ||
} | ||
|
||
@RestController | ||
@RequestMapping | ||
public static class TestController { | ||
@PostMapping(path = "/test", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public String test(@RequestBody String openapi) { | ||
return openapi; | ||
} | ||
} | ||
|
||
@ComponentScan(basePackages = "com.alibaba.fastjson2.springdoc") | ||
@Configuration | ||
@Order(Ordered.LOWEST_PRECEDENCE + 1) | ||
@EnableWebMvc | ||
public static class WebMvcConfig | ||
implements WebMvcConfigurer { | ||
@Override | ||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | ||
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); | ||
converters.add(converter); | ||
} | ||
} | ||
} |