Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
fix: manage '/' contextpath for Virtualhost
Browse files Browse the repository at this point in the history
  • Loading branch information
phiz71 committed Jul 15, 2021
1 parent ae771d7 commit 6d7b601
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
Expand Up @@ -29,6 +29,8 @@
*/
public class VirtualHostDeserializer extends StdScalarDeserializer<VirtualHost> {

private static final String URI_PATH_SEPARATOR = "/";

public VirtualHostDeserializer(Class<VirtualHost> vc) {
super(vc);
}
Expand All @@ -49,7 +51,7 @@ public VirtualHost deserialize(JsonParser jp, DeserializationContext ctxt)
if (pathNode != null) {
vhost.setPath(formatContextPath(pathNode.asText()));
} else {
vhost.setPath("/");
vhost.setPath(URI_PATH_SEPARATOR);
}

vhost.setOverrideEntrypoint(node.path("override_entrypoint").asBoolean(false));
Expand All @@ -58,15 +60,18 @@ public VirtualHost deserialize(JsonParser jp, DeserializationContext ctxt)
}

private String formatContextPath(String contextPath) {
String [] parts = contextPath.split("/");
StringBuilder finalPath = new StringBuilder("/");
String[] parts = contextPath.split(URI_PATH_SEPARATOR);
StringBuilder finalPath = new StringBuilder();

for(String part : parts) {
if (! part.isEmpty()) {
finalPath.append(part).append('/');
if (parts.length > 0) {
for (String part : parts) {
if (!part.isEmpty()) {
finalPath.append(URI_PATH_SEPARATOR).append(part);
}
}
return finalPath.toString();
}

return finalPath.deleteCharAt(finalPath.length() - 1).toString();
return URI_PATH_SEPARATOR;
}
}
}
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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
*
* http://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 io.gravitee.definition.jackson.api;

import io.gravitee.definition.jackson.AbstractTest;
import io.gravitee.definition.model.*;
import org.junit.Assert;
import org.junit.Test;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class VirtualHostDeserializerTest extends AbstractTest {

@Test
public void test_with_default_path() throws Exception {
final String virtualHost = "{\"path\" : \"/\"}";

VirtualHost vHost = objectMapper().readValue(virtualHost, VirtualHost.class);

Assert.assertNull(vHost.getHost());
Assert.assertEquals("/", vHost.getPath());
}

@Test
public void test_with_random_path() throws Exception {
final String virtualHost = "{\"path\" : \"/randomPath\"}";

VirtualHost vHost = objectMapper().readValue(virtualHost, VirtualHost.class);

Assert.assertNull(vHost.getHost());
Assert.assertEquals("/randomPath", vHost.getPath());
}
}

0 comments on commit 6d7b601

Please sign in to comment.