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

com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder Maven / Gradle / Ivy

Go to download

Java JSON Schema Generator – creating a JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from your Java classes

There is a newer version: 4.37.0
Show newest version
/*
 * Copyright 2019 VicTools.
 *
 * 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 com.github.victools.jsonschema.generator;

import com.fasterxml.classmate.AnnotationInclusion;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.victools.jsonschema.generator.impl.SchemaGeneratorConfigImpl;
import java.lang.annotation.Annotation;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Builder class for creating a configuration object to be passed into the SchemaGenerator's constructor.
 */
public class SchemaGeneratorConfigBuilder {

    /**
     * Instantiate an ObjectMapper to be used in case no specific instance is provided in constructor.
     *
     * @return default ObjectMapper instance
     */
    private static ObjectMapper createDefaultObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.getSerializationConfig()
                // since version 4.21.0
                .with(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS);
        // since version 4.25.0; as the above doesn't always work
        mapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));

        return mapper;
    }

    private final ObjectMapper objectMapper;
    private final OptionPreset preset;
    private final SchemaVersion schemaVersion;

    private final Map options = new HashMap<>();
    private final SchemaGeneratorGeneralConfigPart typesInGeneralConfigPart = new SchemaGeneratorGeneralConfigPart();
    private final SchemaGeneratorConfigPart fieldConfigPart = new SchemaGeneratorConfigPart<>();
    private final SchemaGeneratorConfigPart methodConfigPart = new SchemaGeneratorConfigPart<>();
    private final Map, AnnotationInclusion> annotationInclusionOverrides = new LinkedHashMap<>();

    /**
     * Constructor of an empty configuration builder for a {@link SchemaVersion#DRAFT_7 Draft 7} schema. This is equivalent to calling:
* {@code new SchemaGeneratorConfigBuilder(objectMapper, SchemaVersion.DRAFT_7, OptionPreset.FULL_DOCUMENTATION)} * * @param objectMapper supplier for object and array nodes for the JSON structure being generated * @see #SchemaGeneratorConfigBuilder(ObjectMapper, SchemaVersion, OptionPreset) * @deprecated use {@link #SchemaGeneratorConfigBuilder(ObjectMapper, SchemaVersion)} instead */ @Deprecated public SchemaGeneratorConfigBuilder(ObjectMapper objectMapper) { this(objectMapper, SchemaVersion.DRAFT_7, OptionPreset.FULL_DOCUMENTATION); } /** * Constructor of an empty configuration builder. This is equivalent to calling:
* {@code new SchemaGeneratorConfigBuilder(objectMapper, schemaVersion, OptionPreset.FULL_DOCUMENTATION)} * * @param objectMapper supplier for object and array nodes for the JSON structure being generated * @param schemaVersion designated JSON Schema version * @see #SchemaGeneratorConfigBuilder(ObjectMapper, SchemaVersion, OptionPreset) */ public SchemaGeneratorConfigBuilder(ObjectMapper objectMapper, SchemaVersion schemaVersion) { this(objectMapper, schemaVersion, OptionPreset.FULL_DOCUMENTATION); } /** * Constructor of an empty configuration builder with a default {@link ObjectMapper} instance. This is equivalent to calling:
* {@code new SchemaGeneratorConfigBuilder(schemaVersion, OptionPreset.FULL_DOCUMENTATION)} * * @param schemaVersion designated JSON Schema version * @see #SchemaGeneratorConfigBuilder(ObjectMapper, SchemaVersion, OptionPreset) */ public SchemaGeneratorConfigBuilder(SchemaVersion schemaVersion) { this(createDefaultObjectMapper(), schemaVersion, OptionPreset.FULL_DOCUMENTATION); } /** * Constructor of an empty configuration builder for a {@link SchemaVersion#DRAFT_7 Draft 7} schema. This is equivalent to calling:
* {@code new SchemaGeneratorConfigBuilder(objectMapper, SchemaVersion.DRAFT_7, preset)} * * @param objectMapper supplier for object and array nodes for the JSON structure being generated * @param preset default settings for standard {@link Option} values * @deprecated use {@link #SchemaGeneratorConfigBuilder(ObjectMapper, SchemaVersion, OptionPreset)} instead */ @Deprecated public SchemaGeneratorConfigBuilder(ObjectMapper objectMapper, OptionPreset preset) { this(objectMapper, SchemaVersion.DRAFT_7, preset); } /** * Constructor of an empty configuration builder with a default {@link ObjectMapper} instance. * * @param schemaVersion designated JSON Schema version * @param preset default settings for standard {@link Option} values */ public SchemaGeneratorConfigBuilder(SchemaVersion schemaVersion, OptionPreset preset) { this(createDefaultObjectMapper(), schemaVersion, preset); } /** * Constructor of an empty configuration builder. * * @param objectMapper supplier for object and array nodes for the JSON structure being generated * @param schemaVersion designated JSON Schema version * @param preset default settings for standard {@link Option} values */ public SchemaGeneratorConfigBuilder(ObjectMapper objectMapper, SchemaVersion schemaVersion, OptionPreset preset) { this.objectMapper = objectMapper; this.schemaVersion = schemaVersion; this.preset = preset; } /** * Create a schema generator instance from the builder. * * @return successfully created/initialised generator instance */ public SchemaGeneratorConfig build() { // apply the configurations associated with enabled/disabled options EnumSet




© 2015 - 2025 Weber Informatics LLC | Privacy Policy