Search in sources :

Example 1 with TypeContext

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

the class DefaultSchemaDefinitionNamingStrategy method getDefinitionNameForKey.

@Override
public String getDefinitionNameForKey(DefinitionKey key, SchemaGenerationContext generationContext) {
    TypeContext typeContext = generationContext.getTypeContext();
    ResolvedType type = key.getType();
    return typeContext.getSimpleTypeDescription(type);
}
Also used : TypeContext(com.github.victools.jsonschema.generator.TypeContext) ResolvedType(com.fasterxml.classmate.ResolvedType)

Example 2 with TypeContext

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

the class AbstractTypeAwareTest method prepareContextForVersion.

/**
 * Override generation context mock methods that are version dependent.
 *
 * @param schemaVersion designated JSON Schema version
 */
protected void prepareContextForVersion(SchemaVersion schemaVersion) {
    TypeContext typeContext = TypeContextFactory.createDefaultTypeContext();
    ResolvedType resolvedTestClass = typeContext.resolve(this.testClass);
    this.testClassMembers = typeContext.resolveWithMembers(resolvedTestClass);
    this.context = Mockito.mock(SchemaGenerationContext.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(this.context.getTypeContext()).thenReturn(typeContext);
    SchemaGeneratorConfig config = Mockito.mock(SchemaGeneratorConfig.class);
    Mockito.when(this.context.getGeneratorConfig()).thenReturn(config);
    Mockito.when(config.resolveArrayMaxItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMaxItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMaxItemsForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveArrayMinItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMinItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMinItemsForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItemsForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveStringMaxLength(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMaxLength(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMaxLengthForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveStringMinLength(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMinLength(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMinLengthForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.getSchemaVersion()).thenReturn(schemaVersion);
    Answer<String> keywordLookup = invocation -> ((SchemaKeyword) invocation.getArgument(0)).forVersion(schemaVersion);
    Mockito.when(config.getKeyword(Mockito.any())).thenAnswer(keywordLookup);
    Mockito.when(this.context.getKeyword(Mockito.any())).thenAnswer(keywordLookup);
    ObjectMapper objectMapper = new ObjectMapper();
    Mockito.when(config.getObjectMapper()).thenReturn(objectMapper);
    Mockito.when(config.createArrayNode()).thenAnswer((_invocation) -> objectMapper.createArrayNode());
    Mockito.when(config.createObjectNode()).thenAnswer((_invocation) -> objectMapper.createObjectNode());
    Mockito.when(this.context.createStandardDefinitionReference(Mockito.any(ResolvedType.class), Mockito.any())).thenAnswer((_invocation) -> objectMapper.createObjectNode());
}
Also used : TypeContext(com.github.victools.jsonschema.generator.TypeContext) FieldScope(com.github.victools.jsonschema.generator.FieldScope) MethodScope(com.github.victools.jsonschema.generator.MethodScope) TypeContextFactory(com.github.victools.jsonschema.generator.impl.TypeContextFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaGenerationContext(com.github.victools.jsonschema.generator.SchemaGenerationContext) ResolvedTypeWithMembers(com.fasterxml.classmate.ResolvedTypeWithMembers) ResolvedType(com.fasterxml.classmate.ResolvedType) SchemaVersion(com.github.victools.jsonschema.generator.SchemaVersion) ResolvedMethod(com.fasterxml.classmate.members.ResolvedMethod) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Stream(java.util.stream.Stream) SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) ResolvedField(com.fasterxml.classmate.members.ResolvedField) SchemaKeyword(com.github.victools.jsonschema.generator.SchemaKeyword) SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) SchemaGenerationContext(com.github.victools.jsonschema.generator.SchemaGenerationContext) FieldScope(com.github.victools.jsonschema.generator.FieldScope) MethodScope(com.github.victools.jsonschema.generator.MethodScope) SchemaKeyword(com.github.victools.jsonschema.generator.SchemaKeyword) TypeContext(com.github.victools.jsonschema.generator.TypeContext) ResolvedType(com.fasterxml.classmate.ResolvedType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with TypeContext

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

the class JsonSubTypesResolverLookUpTest method testFindSubtypes.

@Test
@Parameters
public void testFindSubtypes(Class<?> targetType, List<Class<?>> erasedSubtypes) {
    this.prepareContextForVersion(SchemaVersion.DRAFT_2019_09);
    TypeContext typeContext = this.getContext().getTypeContext();
    List<ResolvedType> subtypes = this.instance.findSubtypes(typeContext.resolve(targetType), this.getContext());
    this.assertErasedSubtypesAreEquals(erasedSubtypes, subtypes);
}
Also used : TypeContext(com.github.victools.jsonschema.generator.TypeContext) ResolvedType(com.fasterxml.classmate.ResolvedType) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 4 with TypeContext

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

the class AdditionalPropertiesModuleTest method testResolveAdditionalProperties.

@Test
@Parameters
@SuppressWarnings("unchecked")
public void testResolveAdditionalProperties(Module moduleInstance, Type expectedAdditionalProperties, Type type, Type[] typeParameters) {
    moduleInstance.applyToConfigBuilder(this.builder);
    ArgumentCaptor<ConfigFunction<TypeScope, Type>> resolverCaptor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.generalConfigPart).withAdditionalPropertiesResolver(resolverCaptor.capture());
    ConfigFunction<TypeScope, Type> additionalPropertiesResolver = resolverCaptor.getValue();
    TypeContext typeContext = this.getContext().getTypeContext();
    TypeScope scope = typeContext.createTypeScope(typeContext.resolve(type, typeParameters));
    Type result = additionalPropertiesResolver.apply(scope);
    if (expectedAdditionalProperties == null || expectedAdditionalProperties == Void.class) {
        Assert.assertEquals(expectedAdditionalProperties, result);
    } else {
        Assert.assertEquals(typeContext.resolve(expectedAdditionalProperties), result);
    }
}
Also used : Type(java.lang.reflect.Type) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) TypeScope(com.github.victools.jsonschema.generator.TypeScope) TypeContext(com.github.victools.jsonschema.generator.TypeContext) Parameters(junitparams.Parameters) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 5 with TypeContext

use of com.github.victools.jsonschema.generator.TypeContext in project alfresco-java-sdk by Alfresco.

the class JsonSchemaGeneratorTest method getConfigBuilder.

private SchemaGeneratorConfigBuilder getConfigBuilder(final EventEntry entry) {
    final JacksonModule module = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_ORDER);
    final SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON).with(module);
    configBuilder.forTypesInGeneral().withSubtypeResolver((declaredType, generationContext) -> {
        if (declaredType.getErasedType() == Resource.class) {
            final TypeContext typeContext = generationContext.getTypeContext();
            switch(entry) {
                case NODE_ENTRY:
                    return List.of(typeContext.resolveSubtype(declaredType, NodeResource.class));
                case CHILD_ASSOC_ENTRY:
                    return List.of(typeContext.resolveSubtype(declaredType, ChildAssociationResource.class));
                case PEER_ASSOC_ENTRY:
                    return List.of(typeContext.resolveSubtype(declaredType, PeerAssociationResource.class));
            }
        }
        if (declaredType.getErasedType() == DataAttributes.class) {
            final TypeContext typeContext = generationContext.getTypeContext();
            if (entry == EventEntry.NODE_UPDATED_ENTRY) {
                return List.of(typeContext.resolveSubtype(declaredType, EventDataWithResourceBefore.class));
            } else {
                return List.of(typeContext.resolveSubtype(declaredType, EventDataWithoutResourceBefore.class));
            }
        }
        return null;
    });
    configBuilder.forFields().withTargetTypeOverridesResolver(field -> {
        if (field.getDeclaredType().getErasedType() == URI.class) {
            return Stream.of(String.class).map(specificSubtype -> field.getContext().resolve(String.class)).collect(Collectors.toList());
        }
        return null;
    });
    configBuilder.forFields().withRequiredCheck(field -> {
        final Required annotation = field.getAnnotation(Required.class);
        return (annotation != null);
    });
    return configBuilder;
}
Also used : NodeResource(org.alfresco.event.sdk.model.v1.model.schema.NodeResource) TypeContext(com.github.victools.jsonschema.generator.TypeContext) Required(org.alfresco.event.sdk.model.v1.model.schema.Required) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) SchemaVersion(com.github.victools.jsonschema.generator.SchemaVersion) NodeResource(org.alfresco.event.sdk.model.v1.model.schema.NodeResource) RepoEvent(org.alfresco.event.sdk.model.v1.model.schema.RepoEvent) OutputStreamWriter(java.io.OutputStreamWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) PrintWriter(java.io.PrintWriter) DataAttributes(org.alfresco.event.sdk.model.v1.model.DataAttributes) OptionPreset(com.github.victools.jsonschema.generator.OptionPreset) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) EventDataWithResourceBefore(org.alfresco.event.sdk.model.v1.model.schema.EventDataWithResourceBefore) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) EventDataWithoutResourceBefore(org.alfresco.event.sdk.model.v1.model.schema.EventDataWithoutResourceBefore) List(java.util.List) Stream(java.util.stream.Stream) JacksonOption(com.github.victools.jsonschema.module.jackson.JacksonOption) SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) ChildAssociationResource(org.alfresco.event.sdk.model.v1.model.schema.ChildAssociationResource) Resource(org.alfresco.event.sdk.model.v1.model.Resource) PeerAssociationResource(org.alfresco.event.sdk.model.v1.model.schema.PeerAssociationResource) JacksonModule(com.github.victools.jsonschema.module.jackson.JacksonModule) PeerAssociationResource(org.alfresco.event.sdk.model.v1.model.schema.PeerAssociationResource) Required(org.alfresco.event.sdk.model.v1.model.schema.Required) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) ChildAssociationResource(org.alfresco.event.sdk.model.v1.model.schema.ChildAssociationResource) EventDataWithoutResourceBefore(org.alfresco.event.sdk.model.v1.model.schema.EventDataWithoutResourceBefore) TypeContext(com.github.victools.jsonschema.generator.TypeContext) JacksonModule(com.github.victools.jsonschema.module.jackson.JacksonModule) EventDataWithResourceBefore(org.alfresco.event.sdk.model.v1.model.schema.EventDataWithResourceBefore)

Aggregations

TypeContext (com.github.victools.jsonschema.generator.TypeContext)6 ResolvedType (com.fasterxml.classmate.ResolvedType)4 Stream (java.util.stream.Stream)3 Test (org.junit.Test)3 SchemaGenerationContext (com.github.victools.jsonschema.generator.SchemaGenerationContext)2 SchemaGeneratorConfig (com.github.victools.jsonschema.generator.SchemaGeneratorConfig)2 SchemaVersion (com.github.victools.jsonschema.generator.SchemaVersion)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Parameters (junitparams.Parameters)2 ResolvedTypeWithMembers (com.fasterxml.classmate.ResolvedTypeWithMembers)1 ResolvedField (com.fasterxml.classmate.members.ResolvedField)1 ResolvedMethod (com.fasterxml.classmate.members.ResolvedMethod)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AbstractTypeAwareTest (com.github.victools.jsonschema.generator.AbstractTypeAwareTest)1 ConfigFunction (com.github.victools.jsonschema.generator.ConfigFunction)1 FieldScope (com.github.victools.jsonschema.generator.FieldScope)1 MethodScope (com.github.victools.jsonschema.generator.MethodScope)1 OptionPreset (com.github.victools.jsonschema.generator.OptionPreset)1