Skip to content

Commit

Permalink
Merge branch 'hotfix/fixObjectNull'
Browse files Browse the repository at this point in the history
fixing issues when example object value is null``
  • Loading branch information
dekaulitz committed Feb 27, 2020
2 parents 22e56e9 + 1be2b95 commit ad55337
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -68,7 +69,7 @@ public MockExample getMockResponse(PathItem pathItem, HttpServletRequest request
if (mock != null) return mock;
break;
case MockExample.X_DEFAULT:
mock = MockExample.generateResponseDefault(extension.getValue());
mock = MockExample.generateResponseDefault((LinkedHashMap<String,Object>)extension.getValue());
if (mock != null) return mock;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.github.dekaulitz.mockyup.errorhandlers.InvalidMockException;
import com.github.dekaulitz.mockyup.models.MockResponseModel;
import com.github.dekaulitz.mockyup.utils.JsonMapper;
import io.swagger.util.Json;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import org.springframework.http.HttpMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -46,7 +47,9 @@ public class MockExample {
*/
public static MockExample generateResponseHeader(HttpServletRequest request, List<Map<String, Object>> extension) throws InvalidMockException {
for (Map<String, Object> stringObjectMap : extension) {
MockExample mockExample = Json.mapper().convertValue(stringObjectMap, MockExample.class);
MockExample mockExample= new MockExample();
mockExample.setProperty(JsonMapper.mapper().convertValue(stringObjectMap.get("property"), Map.class));
mockExample.setResponse(JsonMapper.mapper().convertValue(stringObjectMap.get("response"), MockResponseModel.class));
throwInvalidMockExample(mockExample, X_HEADERS);
String requestHeader = request.getHeader((String) mockExample.getProperty().get(MockExample.NAME_PROPERTY));
if (requestHeader != null) {
Expand Down Expand Up @@ -75,7 +78,9 @@ public static MockExample generateResponseBody(HttpServletRequest request, List<
|| request.getMethod().equals(HttpMethod.PUT.toString()))
for (Map<String, Object> stringObjectMap : extension) {
JsonNode requestBody = Json.mapper().readTree(body);
MockExample mockExample = Json.mapper().convertValue(stringObjectMap, MockExample.class);
MockExample mockExample= new MockExample();
mockExample.setProperty(JsonMapper.mapper().convertValue(stringObjectMap.get("property"), Map.class));
mockExample.setResponse(JsonMapper.mapper().convertValue(stringObjectMap.get("response"), MockResponseModel.class));
throwInvalidMockExample(mockExample, X_BODY);
String bodyRequest = requestBody.findPath((String) mockExample.getProperty().get(MockExample.NAME_PROPERTY)).asText();
if (!bodyRequest.isEmpty()) {
Expand All @@ -101,7 +106,10 @@ public static MockExample generateResponseBody(HttpServletRequest request, List<
*/
public static MockExample generateResponnsePath(HttpServletRequest request, List<Map<String, Object>> extension, String[] openAPIPaths, String[] paths) throws InvalidMockException {
for (Map<String, Object> stringObjectMap : extension) {
MockExample mockExample = Json.mapper().convertValue(stringObjectMap, MockExample.class);
MockExample mockExample= new MockExample();
mockExample.setProperty(JsonMapper.mapper().convertValue(stringObjectMap.get("property"), Map.class));
mockExample.setResponse(JsonMapper.mapper().convertValue(stringObjectMap.get("response"), MockResponseModel.class));

throwInvalidMockExample(mockExample, X_PATH);
for (int i = 0; i < openAPIPaths.length; i++) {
if (!openAPIPaths[i].equals(paths[i])) {
Expand All @@ -124,7 +132,9 @@ public static MockExample generateResponnsePath(HttpServletRequest request, List
*/
public static MockExample generateResponnseQuery(HttpServletRequest request, List<Map<String, Object>> extension) throws InvalidMockException, UnsupportedEncodingException {
for (Map<String, Object> stringObjectMap : extension) {
MockExample mockExample = Json.mapper().convertValue(stringObjectMap, MockExample.class);
MockExample mockExample= new MockExample();
mockExample.setProperty(JsonMapper.mapper().convertValue(stringObjectMap.get("property"), Map.class));
mockExample.setResponse(JsonMapper.mapper().convertValue(stringObjectMap.get("response"), MockResponseModel.class));
throwInvalidMockExample(mockExample, X_QUERY);
String cleanQueryString = java.net.URLDecoder.decode(request.getQueryString(), String.valueOf(StandardCharsets.UTF_8));
String[] queryStrings = cleanQueryString.split("\\?");
Expand Down Expand Up @@ -159,8 +169,10 @@ public static MockExample generateResponnseQuery(HttpServletRequest request, Lis
* @return
* @desc generate default response
*/
public static MockExample generateResponseDefault(Object value) throws InvalidMockException {
MockExample mockExample = Json.mapper().convertValue(value, MockExample.class);
public static MockExample generateResponseDefault(LinkedHashMap<String,Object> value) throws InvalidMockException {
MockExample mockExample= new MockExample();
mockExample.setProperty(JsonMapper.mapper().convertValue(value.get("property"), Map.class));
mockExample.setResponse(JsonMapper.mapper().convertValue(value.get("response"), MockResponseModel.class));
throwInvalidMockExample(mockExample, X_DEFAULT);
return mockExample;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.github.dekaulitz.mockyup.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.StreamReadFeature;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.v3.parser.ObjectMapperFactory;

public class JsonMapper {
Expand All @@ -12,11 +19,12 @@ public static com.fasterxml.jackson.databind.ObjectMapper mapper() {
if (mapper == null) {
mapper = ObjectMapperFactory.createJson();
}
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
return mapper;
}

public static ObjectWriter pretty() {
return mapper().writer(new DefaultPrettyPrinter());
}

}
}

0 comments on commit ad55337

Please sign in to comment.