Search in sources :

Example 1 with PluginProperty

use of io.kestra.core.models.annotations.PluginProperty in project kestra by kestra-io.

the class JsonSchemaGenerator method build.

protected <T> void build(SchemaGeneratorConfigBuilder builder, Class<? extends T> cls) {
    builder.with(new JacksonModule()).with(new JavaxValidationModule(JavaxValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED, JavaxValidationOption.INCLUDE_PATTERN_EXPRESSIONS)).with(new Swagger2Module()).with(Option.DEFINITIONS_FOR_ALL_OBJECTS).with(Option.DEFINITION_FOR_MAIN_SCHEMA).with(Option.PLAIN_DEFINITION_KEYS).with(Option.ALLOF_CLEANUP_AT_THE_END);
    // default value
    builder.forFields().withDefaultResolver(this::defaults);
    // def name
    builder.forTypesInGeneral().withDefinitionNamingStrategy(new DefaultSchemaDefinitionNamingStrategy() {

        @Override
        public String getDefinitionNameForKey(DefinitionKey key, SchemaGenerationContext context) {
            TypeContext typeContext = context.getTypeContext();
            ResolvedType type = key.getType();
            return typeContext.getFullTypeDescription(type);
        }

        @Override
        public String adjustNullableName(DefinitionKey key, String definitionName, SchemaGenerationContext context) {
            return definitionName;
        }
    });
    // inline some type
    builder.forTypesInGeneral().withCustomDefinitionProvider(new CustomDefinitionProviderV2() {

        @Override
        public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
            if (javaType.isInstanceOf(Map.class) || javaType.isInstanceOf(Enum.class)) {
                ObjectNode definition = context.createStandardDefinition(javaType, this);
                return new CustomDefinition(definition, true);
            } else if (javaType.isInstanceOf(Duration.class)) {
                ObjectNode definitionReference = context.createDefinitionReference(context.getTypeContext().resolve(String.class)).put("format", "duration");
                return new CustomDefinition(definitionReference, true);
            } else {
                return null;
            }
        }
    });
    // PluginProperty $dynamic && deprecated swagger properties
    builder.forFields().withInstanceAttributeOverride((memberAttributes, member, context) -> {
        PluginProperty pluginPropertyAnnotation = member.getAnnotation(PluginProperty.class);
        if (pluginPropertyAnnotation != null) {
            memberAttributes.put("$dynamic", pluginPropertyAnnotation.dynamic());
        }
        Schema schema = member.getAnnotation(Schema.class);
        if (schema != null && schema.deprecated()) {
            memberAttributes.put("$deprecated", true);
        }
    });
    // Add Plugin annotation special docs
    builder.forTypesInGeneral().withTypeAttributeOverride((collectedTypeAttributes, scope, context) -> {
        Plugin pluginAnnotation = scope.getType().getErasedType().getAnnotation(Plugin.class);
        if (pluginAnnotation != null) {
            List<ObjectNode> examples = Arrays.stream(pluginAnnotation.examples()).map(example -> context.getGeneratorConfig().createObjectNode().put("full", example.full()).put("code", String.join("\n", example.code())).put("lang", example.lang()).put("title", example.title())).collect(Collectors.toList());
            if (examples.size() > 0) {
                collectedTypeAttributes.set("$examples", context.getGeneratorConfig().createArrayNode().addAll(examples));
            }
        }
    });
    // PluginProperty additionalProperties
    builder.forFields().withAdditionalPropertiesResolver(target -> {
        PluginProperty pluginPropertyAnnotation = target.getAnnotation(PluginProperty.class);
        if (pluginPropertyAnnotation != null) {
            return pluginPropertyAnnotation.additionalProperties();
        }
        return Object.class;
    });
}
Also used : JavaxValidationModule(com.github.victools.jsonschema.module.javax.validation.JavaxValidationModule) java.util(java.util) com.github.victools.jsonschema.generator(com.github.victools.jsonschema.generator) Plugin(io.kestra.core.models.annotations.Plugin) JacksonMapper(io.kestra.core.serializers.JacksonMapper) JavaxValidationOption(com.github.victools.jsonschema.module.javax.validation.JavaxValidationOption) ResolvedType(com.fasterxml.classmate.ResolvedType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Output(io.kestra.core.models.tasks.Output) Nullable(io.micronaut.core.annotation.Nullable) Duration(java.time.Duration) JsonNode(com.fasterxml.jackson.databind.JsonNode) Schema(io.swagger.v3.oas.annotations.media.Schema) java.lang.reflect(java.lang.reflect) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) DefinitionKey(com.github.victools.jsonschema.generator.impl.DefinitionKey) Singleton(jakarta.inject.Singleton) Swagger2Module(com.github.victools.jsonschema.module.swagger2.Swagger2Module) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) PluginProperty(io.kestra.core.models.annotations.PluginProperty) DefaultSchemaDefinitionNamingStrategy(com.github.victools.jsonschema.generator.naming.DefaultSchemaDefinitionNamingStrategy) Task(io.kestra.core.models.tasks.Task) JacksonModule(com.github.victools.jsonschema.module.jackson.JacksonModule) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(io.swagger.v3.oas.annotations.media.Schema) JavaxValidationModule(com.github.victools.jsonschema.module.javax.validation.JavaxValidationModule) Swagger2Module(com.github.victools.jsonschema.module.swagger2.Swagger2Module) DefaultSchemaDefinitionNamingStrategy(com.github.victools.jsonschema.generator.naming.DefaultSchemaDefinitionNamingStrategy) JacksonModule(com.github.victools.jsonschema.module.jackson.JacksonModule) PluginProperty(io.kestra.core.models.annotations.PluginProperty) DefinitionKey(com.github.victools.jsonschema.generator.impl.DefinitionKey) ResolvedType(com.fasterxml.classmate.ResolvedType) Plugin(io.kestra.core.models.annotations.Plugin)

Aggregations

ResolvedType (com.fasterxml.classmate.ResolvedType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 com.github.victools.jsonschema.generator (com.github.victools.jsonschema.generator)1 DefinitionKey (com.github.victools.jsonschema.generator.impl.DefinitionKey)1 DefaultSchemaDefinitionNamingStrategy (com.github.victools.jsonschema.generator.naming.DefaultSchemaDefinitionNamingStrategy)1 JacksonModule (com.github.victools.jsonschema.module.jackson.JacksonModule)1 JavaxValidationModule (com.github.victools.jsonschema.module.javax.validation.JavaxValidationModule)1 JavaxValidationOption (com.github.victools.jsonschema.module.javax.validation.JavaxValidationOption)1 Swagger2Module (com.github.victools.jsonschema.module.swagger2.Swagger2Module)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Plugin (io.kestra.core.models.annotations.Plugin)1 PluginProperty (io.kestra.core.models.annotations.PluginProperty)1 Output (io.kestra.core.models.tasks.Output)1 Task (io.kestra.core.models.tasks.Task)1 JacksonMapper (io.kestra.core.serializers.JacksonMapper)1 Nullable (io.micronaut.core.annotation.Nullable)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1 Singleton (jakarta.inject.Singleton)1