Search in sources :

Example 1 with CustomPropertyDefinition

use of com.github.victools.jsonschema.generator.CustomPropertyDefinition in project jsonschema-generator by victools.

the class SchemaGenerationContextImplTest method testCreateStandardDefinitionReferenceForField_withCustomPropertyDefinitionExcludingAttributes.

@Test
public void testCreateStandardDefinitionReferenceForField_withCustomPropertyDefinitionExcludingAttributes() {
    ObjectNode customNode = this.contextImpl.getGeneratorConfig().createObjectNode().put("$comment", "custom property");
    Mockito.doReturn(new CustomPropertyDefinition(customNode, CustomDefinition.EXCLUDING_ATTRIBUTES)).when(this.contextImpl.getGeneratorConfig()).getCustomDefinition(Mockito.any(FieldScope.class), Mockito.any(), Mockito.any());
    FieldScope targetField = this.getTestClassField("booleanField");
    ObjectNode result = this.contextImpl.createStandardDefinitionReference(targetField, null);
    Assert.assertEquals("{\"$comment\":\"custom property\"}", result.toString());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FieldScope(com.github.victools.jsonschema.generator.FieldScope) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 2 with CustomPropertyDefinition

use of com.github.victools.jsonschema.generator.CustomPropertyDefinition in project kogito-runtimes by kiegroup.

the class JsonSchemaGenerator method getInputOutput.

private CustomPropertyDefinition getInputOutput(FieldScope scope, SchemaGenerationContext context) {
    UserTaskParam param = scope.getAnnotation(UserTaskParam.class);
    if (param != null) {
        final ObjectNode refNode = context.createStandardDefinitionReference(scope.getDeclaredType(), null);
        ObjectNode rootNode = context.getGeneratorConfig().createObjectNode();
        ArrayNode allOfNode = rootNode.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF));
        allOfNode.add(refNode);
        allOfNode.addObject().put(param.value().toString().toLowerCase(), true);
        return new CustomPropertyDefinition(rootNode, AttributeInclusion.YES);
    }
    return null;
}
Also used : UserTaskParam(org.kie.kogito.UserTaskParam) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition)

Example 3 with CustomPropertyDefinition

use of com.github.victools.jsonschema.generator.CustomPropertyDefinition in project tigris-client-java by tigrisdata.

the class StandardModelToTigrisJsonSchema method toJsonSchema.

@Override
public JsonNode toJsonSchema(Class<? extends TigrisCollectionType> clazz) {
    SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON);
    SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES).with(builder -> builder.forFields().withDescriptionResolver(target -> {
        TigrisField tigrisField = target.getAnnotation(TigrisField.class);
        if (tigrisField != null && !tigrisField.description().isEmpty()) {
            return target.getAnnotation(TigrisField.class).description();
        }
        return null;
    }).withCustomDefinitionProvider((scope, context) -> {
        // one or multiple dimensional byte array
        if (scope.getType().getSignature().endsWith("[B")) {
            ObjectNode customProperty = handleMultiDimensionalByteArray(scope.getType().getSignature());
            return new CustomPropertyDefinition(customProperty);
        }
        return null;
    })).build();
    SchemaGenerator generator = new SchemaGenerator(config);
    JsonNode jsonSchema = generator.generateSchema(clazz);
    JsonNode result = customizeSchema(jsonSchema, clazz);
    log.info("Collection type class: {}, schema: {}", clazz.getName(), result.toPrettyString());
    return result;
}
Also used : Logger(org.slf4j.Logger) Option(com.github.victools.jsonschema.generator.Option) Iterator(java.util.Iterator) OptionPreset(com.github.victools.jsonschema.generator.OptionPreset) TigrisCollectionType(com.tigrisdata.db.type.TigrisCollectionType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) Field(java.lang.reflect.Field) TigrisCollection(com.tigrisdata.db.annotation.TigrisCollection) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SchemaVersion(com.github.victools.jsonschema.generator.SchemaVersion) TigrisField(com.tigrisdata.db.annotation.TigrisField) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) TigrisPrimaryKey(com.tigrisdata.db.annotation.TigrisPrimaryKey) SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) TypeUtils(com.tigrisdata.db.util.TypeUtils) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) TigrisField(com.tigrisdata.db.annotation.TigrisField)

Example 4 with CustomPropertyDefinition

use of com.github.victools.jsonschema.generator.CustomPropertyDefinition in project jsonschema-generator by victools.

the class SchemaGenerationContextImplTest method testCreateStandardDefinitionReferenceForField_withCustomPropertyAndTypeDefinitions.

@Test
public void testCreateStandardDefinitionReferenceForField_withCustomPropertyAndTypeDefinitions() {
    Mockito.doReturn(new CustomDefinition(this.contextImpl.getGeneratorConfig().createObjectNode().put("$comment", "custom type"), true)).when(this.contextImpl.getGeneratorConfig()).getCustomDefinition(Mockito.any(ResolvedType.class), Mockito.any(), Mockito.any());
    Mockito.doReturn(new CustomPropertyDefinition(this.contextImpl.getGeneratorConfig().createObjectNode().put("$comment", "custom property"))).when(this.contextImpl.getGeneratorConfig()).getCustomDefinition(Mockito.any(FieldScope.class), Mockito.any(), Mockito.any());
    FieldScope targetField = this.getTestClassField("booleanField");
    ObjectNode result = this.contextImpl.createStandardDefinitionReference(targetField, null);
    Assert.assertEquals("{\"$comment\":\"custom property\",\"title\":\"Field Title\",\"description\":\"Type Description\"}", result.toString());
}
Also used : CustomDefinition(com.github.victools.jsonschema.generator.CustomDefinition) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FieldScope(com.github.victools.jsonschema.generator.FieldScope) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) ResolvedType(com.fasterxml.classmate.ResolvedType) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 5 with CustomPropertyDefinition

use of com.github.victools.jsonschema.generator.CustomPropertyDefinition in project jsonschema-generator by victools.

the class SchemaGenerationContextImplTest method testCreateStandardDefinitionReferenceForMethod_withCustomPropertyDefinition.

@Test
public void testCreateStandardDefinitionReferenceForMethod_withCustomPropertyDefinition() {
    Mockito.doReturn(new CustomPropertyDefinition(this.contextImpl.getGeneratorConfig().createObjectNode().put("$comment", "custom property"))).when(this.contextImpl.getGeneratorConfig()).getCustomDefinition(Mockito.any(MethodScope.class), Mockito.any(), Mockito.any());
    MethodScope targetMethod = this.getTestClassMethod("isBooleanField");
    JsonNode result = this.contextImpl.createStandardDefinitionReference(targetMethod, null);
    Assert.assertEquals("{\"$comment\":\"custom property\",\"title\":\"Method Title\",\"description\":\"Type Description\"}", result.toString());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) MethodScope(com.github.victools.jsonschema.generator.MethodScope) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Aggregations

CustomPropertyDefinition (com.github.victools.jsonschema.generator.CustomPropertyDefinition)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 AbstractTypeAwareTest (com.github.victools.jsonschema.generator.AbstractTypeAwareTest)5 Test (org.junit.Test)5 FieldScope (com.github.victools.jsonschema.generator.FieldScope)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 CustomDefinition (com.github.victools.jsonschema.generator.CustomDefinition)3 MethodScope (com.github.victools.jsonschema.generator.MethodScope)3 ResolvedType (com.fasterxml.classmate.ResolvedType)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CustomPropertyDefinitionProvider (com.github.victools.jsonschema.generator.CustomPropertyDefinitionProvider)1 Option (com.github.victools.jsonschema.generator.Option)1 OptionPreset (com.github.victools.jsonschema.generator.OptionPreset)1 SchemaGenerator (com.github.victools.jsonschema.generator.SchemaGenerator)1 SchemaGeneratorConfig (com.github.victools.jsonschema.generator.SchemaGeneratorConfig)1 SchemaGeneratorConfigBuilder (com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder)1 SchemaVersion (com.github.victools.jsonschema.generator.SchemaVersion)1