Search in sources :

Example 1 with SchemaVersion

use of com.github.victools.jsonschema.generator.SchemaVersion 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 2 with SchemaVersion

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

the class SchemaGenerationContextImplTest method setUp.

@Before
public void setUp() {
    SchemaVersion schemaVersion = SchemaVersion.DRAFT_2019_09;
    this.prepareContextForVersion(schemaVersion);
    SchemaGeneratorConfig config = Mockito.mock(SchemaGeneratorConfig.class);
    Mockito.when(config.getSchemaVersion()).thenReturn(schemaVersion);
    Mockito.when(config.getKeyword(Mockito.any())).thenAnswer(invocation -> ((SchemaKeyword) invocation.getArgument(0)).forVersion(schemaVersion));
    Mockito.when(config.resolveTitle(Mockito.any(FieldScope.class))).thenReturn("Field Title");
    Mockito.when(config.resolveTitle(Mockito.any(MethodScope.class))).thenReturn("Method Title");
    Mockito.when(config.resolveDescriptionForType(Mockito.any())).thenReturn("Type Description");
    Mockito.when(config.resolveStringMinLength(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMaxLength(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMinItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMaxItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItems(Mockito.any(FieldScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMinLength(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMaxLength(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMinItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayMaxItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItems(Mockito.any(MethodScope.class))).thenReturn(null);
    Mockito.when(config.resolveStringMinLengthForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveStringMaxLengthForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveArrayMinItemsForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveArrayMaxItemsForType(Mockito.any())).thenReturn(null);
    Mockito.when(config.resolveArrayUniqueItemsForType(Mockito.any())).thenReturn(null);
    ObjectMapper objectMapper = new ObjectMapper();
    Mockito.when(config.createObjectNode()).then(_invocation -> objectMapper.createObjectNode());
    Mockito.when(config.createArrayNode()).then(_invocation -> objectMapper.createArrayNode());
    this.contextImpl = new SchemaGenerationContextImpl(config, this.getContext().getTypeContext());
}
Also used : SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) SchemaVersion(com.github.victools.jsonschema.generator.SchemaVersion) FieldScope(com.github.victools.jsonschema.generator.FieldScope) MethodScope(com.github.victools.jsonschema.generator.MethodScope) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 3 with SchemaVersion

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

the class AttributeCollector method setEnum.

/**
 * Setter for "{@link SchemaKeyword#TAG_CONST}"/"{@link SchemaKeyword#TAG_ENUM}" attribute (considering Draft 7).
 *
 * @param node schema node to set attribute on
 * @param enumValues attribute value to set
 * @return this instance (for chaining)
 * @deprecated use {@link #setEnum(ObjectNode, Collection, SchemaGenerationContext)} instead
 */
@Deprecated
public AttributeCollector setEnum(ObjectNode node, Collection<?> enumValues) {
    if (enumValues != null) {
        SchemaVersion schemaVersion = SchemaVersion.DRAFT_7;
        List<Object> values = enumValues.stream().filter(this::isSupportedEnumValue).filter(this::canBeConvertedToString).collect(Collectors.toList());
        if (values.size() == 1) {
            Object singleValue = values.get(0);
            if (singleValue instanceof String) {
                node.put(SchemaKeyword.TAG_CONST.forVersion(schemaVersion), (String) singleValue);
            } else {
                node.putPOJO(SchemaKeyword.TAG_CONST.forVersion(schemaVersion), singleValue);
            }
        } else if (!values.isEmpty()) {
            ArrayNode array = node.arrayNode();
            for (Object singleValue : values) {
                if (singleValue instanceof String) {
                    array.add((String) singleValue);
                } else {
                    array.addPOJO(singleValue);
                }
            }
            node.set(SchemaKeyword.TAG_ENUM.forVersion(schemaVersion), array);
        }
    }
    return this;
}
Also used : SchemaVersion(com.github.victools.jsonschema.generator.SchemaVersion) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

SchemaVersion (com.github.victools.jsonschema.generator.SchemaVersion)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 FieldScope (com.github.victools.jsonschema.generator.FieldScope)2 MethodScope (com.github.victools.jsonschema.generator.MethodScope)2 SchemaGeneratorConfig (com.github.victools.jsonschema.generator.SchemaGeneratorConfig)2 ResolvedType (com.fasterxml.classmate.ResolvedType)1 ResolvedTypeWithMembers (com.fasterxml.classmate.ResolvedTypeWithMembers)1 ResolvedField (com.fasterxml.classmate.members.ResolvedField)1 ResolvedMethod (com.fasterxml.classmate.members.ResolvedMethod)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 SchemaGenerationContext (com.github.victools.jsonschema.generator.SchemaGenerationContext)1 SchemaKeyword (com.github.victools.jsonschema.generator.SchemaKeyword)1 TypeContext (com.github.victools.jsonschema.generator.TypeContext)1 TypeContextFactory (com.github.victools.jsonschema.generator.impl.TypeContextFactory)1 Stream (java.util.stream.Stream)1 Before (org.junit.Before)1 Mockito (org.mockito.Mockito)1 Answer (org.mockito.stubbing.Answer)1