Search in sources :

Example 1 with Constructible

use of org.eclipse.microprofile.openapi.models.Constructible in project wildfly-swarm by wildfly-swarm.

the class OASFactoryResolverImplTest method testCreateObject_All.

/**
 * Test method for
 * {@link org.wildfly.swarm.microprofile.openapi.deployment.spi.OASFactoryResolverImpl#createObject(java.lang.Class)}.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateObject_All() {
    Class[] modelClasses = { APIResponse.class, APIResponses.class, Callback.class, Components.class, Contact.class, Content.class, Discriminator.class, Encoding.class, Example.class, ExternalDocumentation.class, Header.class, Info.class, License.class, Link.class, MediaType.class, OAuthFlow.class, OAuthFlows.class, OpenAPI.class, Operation.class, Parameter.class, PathItem.class, Paths.class, RequestBody.class, Schema.class, Scopes.class, SecurityRequirement.class, SecurityScheme.class, Server.class, ServerVariable.class, ServerVariables.class, Tag.class, XML.class };
    for (Class modelClass : modelClasses) {
        Constructible object = OASFactory.createObject(modelClass);
        Assert.assertNotNull(object);
    }
}
Also used : Constructible(org.eclipse.microprofile.openapi.models.Constructible) Test(org.junit.Test)

Example 2 with Constructible

use of org.eclipse.microprofile.openapi.models.Constructible in project Payara by payara.

the class ModelUtils method merge.

@SuppressWarnings("unchecked")
public static <T> void merge(T from, T to, boolean override) {
    if (from != null && to != null) {
        for (Field f : to.getClass().getDeclaredFields()) {
            // Skip static or synthetic fields
            if (f.isSynthetic() || Modifier.isStatic(f.getModifiers())) {
                continue;
            }
            f.setAccessible(true);
            try {
                // Get the new and old value
                Object fromValue = f.get(from);
                Object toValue = f.get(to);
                // If there is no 'from', ignore
                if (fromValue == null) {
                    continue;
                }
                if (fromValue instanceof Map && toValue != null) {
                    Map<Object, Object> fromMap = (Map<Object, Object>) fromValue;
                    Map<Object, Object> toMap = (Map<Object, Object>) toValue;
                    for (Entry<Object, Object> entry : fromMap.entrySet()) {
                        if (!toMap.containsKey(entry.getKey())) {
                            toMap.put(entry.getKey(), entry.getValue());
                        } else {
                            merge(entry.getValue(), toMap.get(entry.getKey()), override);
                        }
                    }
                } else if (fromValue instanceof Collection && toValue != null) {
                    Collection<Object> fromCollection = (Collection<Object>) fromValue;
                    Collection<Object> toCollection = (Collection<Object>) toValue;
                    for (Object o : fromCollection) {
                        if (!toCollection.contains(o)) {
                            toCollection.add(o);
                        }
                    }
                } else if (fromValue instanceof Constructible) {
                    if (toValue == null) {
                        f.set(to, fromValue.getClass().newInstance());
                        toValue = f.get(to);
                    }
                    merge(fromValue, toValue, override);
                } else {
                    f.set(to, mergeProperty(f.get(to), f.get(from), override));
                }
            } catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
            // Ignore errors
            }
        }
    }
}
Also used : Constructible(org.eclipse.microprofile.openapi.models.Constructible) Field(java.lang.reflect.Field) Collection(java.util.Collection) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 3 with Constructible

use of org.eclipse.microprofile.openapi.models.Constructible in project Payara by payara.

the class ObjectMapperFactory method create.

@SuppressWarnings("unchecked")
public static <T extends Constructible> ObjectMapper create(JsonFactory factory) {
    ObjectMapper mapper = new ObjectMapper(factory);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);
    // Create mapping module
    SimpleModule module = new SimpleModule();
    // Configure the implementation of each class
    for (Entry<Class<? extends Constructible>, Class<? extends Constructible>> entry : OASFactoryResolverImpl.MODEL_MAP.entrySet()) {
        module.addAbstractTypeMapping((Class<T>) entry.getKey(), (Class<T>) entry.getValue());
    }
    // Configure the mixins for each type
    mapper.setMixIns(OASFactoryResolverImpl.MODEL_MAP.keySet().stream().collect(Collectors.toMap(Function.identity(), c -> ExtensionsMixin.class)));
    mapper.registerModule(module);
    return mapper;
}
Also used : Constructible(org.eclipse.microprofile.openapi.models.Constructible) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 4 with Constructible

use of org.eclipse.microprofile.openapi.models.Constructible in project wildfly-swarm by wildfly-swarm.

the class MergeUtil method mergeMaps.

/**
 * Merges two Maps.  Any values missing from Map1 but present in Map2 will be added.  If a value
 * is present in both maps, it will be overridden or merged.
 * @param values1
 * @param values2
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Map mergeMaps(Map values1, Map values2) {
    if (values1 == null && values2 == null) {
        return null;
    }
    if (values1 != null && values2 == null) {
        return values1;
    }
    if (values1 == null && values2 != null) {
        return values2;
    }
    for (Object key : values2.keySet()) {
        if (values1.containsKey(key)) {
            Object pval1 = values1.get(key);
            Object pval2 = values2.get(key);
            if (pval1 instanceof Map) {
                values1.put(key, mergeMaps((Map) pval1, (Map) pval2));
            } else if (pval1 instanceof List) {
                values1.put(key, mergeLists((List) pval1, (List) pval2));
            } else if (pval1 instanceof Constructible) {
                values1.put(key, mergeObjects(pval1, pval2));
            } else {
                values1.put(key, pval2);
            }
        } else {
            Object pval2 = values2.get(key);
            values1.put(key, pval2);
        }
    }
    if (values1 instanceof Constructible) {
        if (values1 instanceof Reference) {
            Reference ref1 = (Reference) values1;
            Reference ref2 = (Reference) values2;
            if (ref2.getRef() != null) {
                ref1.setRef(ref2.getRef());
            }
        }
        if (values1 instanceof Extensible) {
            Extensible extensible1 = (Extensible) values1;
            Extensible extensible2 = (Extensible) values2;
            extensible1.setExtensions(mergeMaps(extensible1.getExtensions(), extensible2.getExtensions()));
        }
        if (values1 instanceof APIResponses) {
            APIResponses responses1 = (APIResponses) values1;
            APIResponses responses2 = (APIResponses) values2;
            responses1.defaultValue(mergeObjects(responses1.getDefault(), responses2.getDefault()));
        }
    }
    return values1;
}
Also used : Constructible(org.eclipse.microprofile.openapi.models.Constructible) Extensible(org.eclipse.microprofile.openapi.models.Extensible) Reference(org.eclipse.microprofile.openapi.models.Reference) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

Constructible (org.eclipse.microprofile.openapi.models.Constructible)4 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 Extensible (org.eclipse.microprofile.openapi.models.Extensible)1 Reference (org.eclipse.microprofile.openapi.models.Reference)1 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)1 Test (org.junit.Test)1