Skip to content

Commit

Permalink
Merge branch 'a1dutch-extra-extension-types'
Browse files Browse the repository at this point in the history
  • Loading branch information
dilipkrish committed Aug 3, 2019
2 parents b3b5166 + a843fff commit f303819
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@


import org.mapstruct.Mapper;
import springfox.documentation.service.ListVendorExtension;
import springfox.documentation.service.ObjectVendorExtension;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.service.VendorExtension;
Expand All @@ -39,21 +38,21 @@ public class VendorExtensionsMapper {

public Map<String, Object> mapExtensions(List<VendorExtension> from) {
Map<String, Object> extensions = new TreeMap<>();
Iterable<ListVendorExtension> listExtensions = from.stream()
.filter(ListVendorExtension.class::isInstance).map(each -> (ListVendorExtension) each).collect(toList());
for (ListVendorExtension each : listExtensions) {
extensions.put(each.getName(), each.getValue());
}
Iterable<Map<String, Object>> objectExtensions = from.stream()
.filter(ObjectVendorExtension.class::isInstance).map(each -> (ObjectVendorExtension) each)
.filter(ObjectVendorExtension.class::isInstance)
.map(each -> (ObjectVendorExtension) each)
.map(toExtensionMap()).collect(toList());
for (Map<String, Object> each : objectExtensions) {
extensions.putAll(each);
}
Iterable<StringVendorExtension> propertyExtensions = from.stream()
.filter(StringVendorExtension.class::isInstance).map(each -> (StringVendorExtension) each).collect(toList());
for (StringVendorExtension each : propertyExtensions) {
extensions.put(each.getName(), each.getValue());

Iterable<VendorExtension> propertyExtensions = from.stream()
.filter(each -> !(each instanceof ObjectVendorExtension))
.collect(toList());
for (VendorExtension each : propertyExtensions) {
extensions.put(
each.getName(),
each.getValue());
}
return extensions;
}
Expand All @@ -62,7 +61,9 @@ private Function<ObjectVendorExtension, Map<String, Object>> toExtensionMap() {
return input -> {
if (!isEmpty(input.getName())) {
Map<String, Object> map = new HashMap<>();
map.put(input.getName(), mapExtensions(input.getValue()));
map.put(
input.getName(),
mapExtensions(input.getValue()));
return map;
}
return propertiesAsMap(input);
Expand All @@ -73,15 +74,19 @@ private Map<String, Object> propertiesAsMap(ObjectVendorExtension input) {
Map<String, Object> properties = new HashMap<>();
Iterable<StringVendorExtension> stringExtensions =
input.getValue().stream().filter(StringVendorExtension.class::isInstance)
.map(each -> (StringVendorExtension) each).collect(toList());
.map(each -> (StringVendorExtension) each).collect(toList());
for (StringVendorExtension property : stringExtensions) {
properties.put(property.getName(), property.getValue());
properties.put(
property.getName(),
property.getValue());
}
Iterable<ObjectVendorExtension> objectExtensions =
input.getValue().stream().filter(ObjectVendorExtension.class::isInstance)
.map(each -> (ObjectVendorExtension) each).collect(toList());
.map(each -> (ObjectVendorExtension) each).collect(toList());
for (ObjectVendorExtension property : objectExtensions) {
properties.put(property.getName(), mapExtensions(property.getValue()));
properties.put(
property.getName(),
mapExtensions(property.getValue()));
}
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
package springfox.documentation.swagger2.mappers

import spock.lang.Specification
import springfox.documentation.service.VendorExtension
import springfox.documentation.service.ListVendorExtension
import springfox.documentation.service.ObjectVendorExtension
import springfox.documentation.service.StringVendorExtension

class VendorExtensionsMapperSpec extends Specification {

class Person {
String name;
int age;
}
def second() {
def second = new ObjectVendorExtension("x-test2")
second.with {
Expand All @@ -45,11 +49,25 @@ class VendorExtensionsMapperSpec extends Specification {
new ListVendorExtension("x-test3", [1, 3])
}

def fourth() {
new VendorExtension() {
def getValue() {
Person person = new Person()
person.name = "test"
person.age = 1
return person
}
String getName() {
"x-test4"
}
}
}

def "mapper works as expected" () {
given:
VendorExtensionsMapper sut = new VendorExtensionsMapper()
when:
def mapped = sut.mapExtensions([first(), second(), third()])
def mapped = sut.mapExtensions([first(), second(), third(), fourth()])
then:
mapped.containsKey("x-test1")
mapped["x-test1"] == "value1"
Expand All @@ -59,5 +77,10 @@ class VendorExtensionsMapperSpec extends Specification {
and:
mapped.containsKey("x-test3")
mapped["x-test3"] == [1, 3]
and:
mapped.containsKey("x-test4")
Person p = mapped["x-test4"]
p.name == "test"
p.age == 1
}
}

0 comments on commit f303819

Please sign in to comment.