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

org.deltafi.actionkit.action.util.ActionParameterSchemaGenerator Maven / Gradle / Ivy

There is a newer version: 2.0-rc2
Show newest version
/*
 *    DeltaFi - Data transformation and enrichment platform
 *
 *    Copyright 2021-2023 DeltaFi Contributors 
 *
 *    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 org.deltafi.actionkit.action.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.*;
import com.github.victools.jsonschema.module.jackson.JacksonModule;
import com.github.victools.jsonschema.module.jackson.JacksonOption;
import org.deltafi.actionkit.action.parameters.ActionParameters;

import java.util.Map;

/**
 * Helper class that provides a generic schema generation function for arbitrary parameter classes that are used to
 * configure actions
 *
 * @see ActionParameters
 */
public class ActionParameterSchemaGenerator {

    private static final SchemaGenerator SCHEMA_GENERATOR;

    static {
        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
                .without(Option.SCHEMA_VERSION_INDICATOR)
                .with(Option.FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT)
                .with(Option.INLINE_ALL_SCHEMAS)
                .with(new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, JacksonOption.IGNORE_TYPE_INFO_TRANSFORM, JacksonOption.FLATTENED_ENUMS_FROM_JSONPROPERTY));

        configBuilder.forTypesInGeneral()
                .withAdditionalPropertiesResolver(scope -> {
                    if (scope.getType().isInstanceOf(Map.class)) {
                        return scope.getTypeParameterFor(Map.class, 1);
                    }
                    return null;
                });

        SCHEMA_GENERATOR = new SchemaGenerator(configBuilder.build());
    }

    private ActionParameterSchemaGenerator() {

    }

    public static JsonNode generateSchema(Class clazz) {
        return SCHEMA_GENERATOR.generateSchema(clazz);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy