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

Fix spring-doc issue #387 #406

Merged
merged 1 commit into from
Jun 4, 2022
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
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);
}
}
}