All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.openapiprocessor.jsonschema.converter.MapDependencyConverter Maven / Gradle / Ivy

There is a newer version: 2024.4
Show newest version
/*
 * Copyright 2022 https://github.com/openapi-processor/openapi-parser
 * PDX-License-Identifier: Apache-2.0
 */

package io.openapiprocessor.jsonschema.converter;

import io.openapiprocessor.jsonschema.schema.JsonDependency;
import io.openapiprocessor.jsonschema.schema.JsonPointer;
import io.openapiprocessor.jsonschema.schema.JsonSchema;
import io.openapiprocessor.jsonschema.schema.JsonSchemaContext;
import io.openapiprocessor.jsonschema.support.Types;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import static io.openapiprocessor.jsonschema.support.Null.nonNull;

/**
 * get a map of {@link String} to {@link JsonDependency}.
 */
public class MapDependencyConverter implements PropertyConverter> {
    private final JsonSchemaContext parentContext;

    public MapDependencyConverter (JsonSchemaContext parentContext) {
        this.parentContext = parentContext;
    }

    @Override
    public @Nullable Map convert (
        String name, @Nullable Object value, String location)
    {
        Map objects = Types.convertMapOrNull (location, value);
        if (objects == null)
            return null;

        JsonPointer parentLocation = JsonPointer.from (location);

        Map result = new LinkedHashMap<> ();
        objects.forEach ((propKey, propValue) -> {
            if (Types.isObject(propValue) || Types.isBoolean (propValue)) {
                result.put (propKey, new JsonDependency (
                    create (name, propValue, getLocation (parentLocation, propKey)))
                );
            } else if (Types.isArray (propValue)) {
                Collection property = Types.asCol (propValue);
                result.put (propKey, new JsonDependency (property));
            }
        });

        return Collections.unmodifiableMap (result);
    }

    private JsonSchema create (String name, Object value, String location) {
        return nonNull (new JsonSchemaConverter (parentContext).convert (name, value, location));
    }

    private String getLocation (JsonPointer parent, String property) {
        return parent.getJsonPointer (property);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy