Skip to content

Commit

Permalink
fix spring-doc issue alibaba#387.
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorZeng committed Jun 4, 2022
1 parent e77e1ed commit 7038d34
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,18 @@ private Object readType(Type type, HttpInputMessage inputMessage) {
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
HttpHeaders headers = outputMessage.getHeaders();

int len = JSON.writeTo(baos, object, config.getDateFormat(), config.getWriterFilters(), config.getWriterFeatures());
int contentLength;

if (object instanceof String && JSON.isValidObject((String) object)) {
byte[] strBytes = ((String) object).getBytes(config.getCharset());
contentLength = strBytes.length;
baos.write(strBytes, 0, strBytes.length);
} else {
contentLength = JSON.writeTo(baos, object, config.getDateFormat(), config.getWriterFilters(), config.getWriterFeatures());
}

if (headers.getContentLength() < 0 && config.isWriteContentLength()) {
headers.setContentLength(len);
headers.setContentLength(contentLength);
}

baos.writeTo(outputMessage.getBody());
Expand Down

This file was deleted.

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);
}
}
}

0 comments on commit 7038d34

Please sign in to comment.