diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/CustomEnumDefinitionProvider.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/CustomEnumDefinitionProvider.java index 17a4ee9c..a68eb535 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/CustomEnumDefinitionProvider.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/CustomEnumDefinitionProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 VicTools. + * Copyright 2024 VicTools. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,7 +141,10 @@ protected List getSerializedValuesFromJsonProperty(ResolvedType javaType List serializedJsonValues = new ArrayList<>(enumConstants.length); for (Object enumConstant : enumConstants) { String enumValueName = ((Enum) enumConstant).name(); - Optional annotation = JacksonHelper.resolveAnnotation(javaType.getErasedType().getDeclaredField(enumValueName), JsonProperty.class); + Optional annotation = JacksonHelper.resolveAnnotation( + javaType.getErasedType().getDeclaredField(enumValueName), + JsonProperty.class + ); if (!annotation.isPresent()) { // enum constant without @JsonProperty annotation return null; diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonHelper.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonHelper.java index db08f84b..ad1d0a62 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonHelper.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonHelper.java @@ -1,8 +1,23 @@ +/* + * Copyright 2024 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.module.jackson; import com.fasterxml.classmate.members.ResolvedMember; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; - import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.util.Arrays; @@ -21,6 +36,7 @@ private static final class JacksonAnnotationsInsideAnnotatedFilter implements Pr JacksonAnnotationsInsideAnnotatedFilter() { super(); } + @Override public boolean test(Annotation annotation) { return annotation.annotationType().isAnnotationPresent(JacksonAnnotationsInside.class); @@ -34,12 +50,14 @@ private JacksonHelper() { /** * Resolves the specified annotation on the given resolved member and resolve indirect jackson annotations. * - *

It uses the same algorithm as {@link com.github.victools.jsonschema.generator.TypeContext#getAnnotationFromList(Class, List, Predicate)}.

+ *

It uses the same algorithm as + * {@link com.github.victools.jsonschema.generator.TypeContext#getAnnotationFromList(Class, List, Predicate)} + * .

* + * @param the generic type of the annotation * @param member where to look for the specified annotation * @param annotationClass the class of the annotation to look for * @return an empty entry if not found - * @param the generic type of the annotation */ static Optional resolveAnnotation(ResolvedMember member, Class annotationClass) { final A annotation = member.getAnnotations().get(annotationClass); @@ -52,12 +70,14 @@ static Optional resolveAnnotation(ResolvedMember me /** * Resolves the specified annotation on the given type and resolve indirect jackson annotations. * - *

It uses the same algorithm as {@link com.github.victools.jsonschema.generator.TypeContext#getAnnotationFromList(Class, List, Predicate)}.

- * + *

It uses the same algorithm as + * {@link com.github.victools.jsonschema.generator.TypeContext#getAnnotationFromList(Class, List, Predicate)} + * .

+ * + * @param
the generic type of the annotation * @param declaringType where to look for the specified annotation * @param annotationClass the class of the annotation to look for * @return an empty entry if not found - * @param the generic type of the annotation */ static Optional resolveAnnotation(AnnotatedElement declaringType, Class annotationClass) { final A annotation = declaringType.getAnnotation(annotationClass); diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java index 8e869329..928e7a32 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JacksonModule.java @@ -198,7 +198,10 @@ protected String resolveDescriptionForType(TypeScope scope) { * @return alternative property name (or {@code null}) */ protected String getPropertyNameOverrideBasedOnJsonPropertyAnnotation(MemberScope member) { - JsonProperty annotation = member.getAnnotationConsideringFieldAndGetter(JsonProperty.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonProperty annotation = member.getAnnotationConsideringFieldAndGetter( + JsonProperty.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (annotation != null) { String nameOverride = annotation.value(); // check for invalid overrides @@ -271,11 +274,16 @@ protected final BeanDescription getBeanDescriptionForClass(ResolvedType targetTy * @return whether field should be excluded */ protected boolean shouldIgnoreField(FieldScope field) { - if (field.getAnnotationConsideringFieldAndGetterIfSupported(JsonBackReference.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) != null) { + if (field.getAnnotationConsideringFieldAndGetterIfSupported( + JsonBackReference.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) != null) { return true; } // @since 4.32.0 - JsonUnwrapped unwrappedAnnotation = field.getAnnotationConsideringFieldAndGetterIfSupported(JsonUnwrapped.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonUnwrapped unwrappedAnnotation = field.getAnnotationConsideringFieldAndGetterIfSupported( + JsonUnwrapped.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (unwrappedAnnotation != null && unwrappedAnnotation.enabled()) { // unwrapped properties should be ignored here, as they are included in their unwrapped form return true; @@ -306,11 +314,16 @@ protected boolean shouldIgnoreField(FieldScope field) { protected boolean shouldIgnoreMethod(MethodScope method) { FieldScope getterField = method.findGetterField(); if (getterField == null) { - if (method.getAnnotationConsideringFieldAndGetterIfSupported(JsonBackReference.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) != null) { + if (method.getAnnotationConsideringFieldAndGetterIfSupported( + JsonBackReference.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) != null) { return true; } // @since 4.32.0 - JsonUnwrapped unwrappedAnnotation = method.getAnnotationConsideringFieldAndGetterIfSupported(JsonUnwrapped.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonUnwrapped unwrappedAnnotation = method.getAnnotationConsideringFieldAndGetterIfSupported( + JsonUnwrapped.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (unwrappedAnnotation != null && unwrappedAnnotation.enabled()) { // unwrapped properties should be ignored here, as they are included in their unwrapped form return true; @@ -319,7 +332,9 @@ protected boolean shouldIgnoreMethod(MethodScope method) { return true; } return this.options.contains(JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS) - && method.getAnnotationConsideringFieldAndGetter(JsonProperty.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) == null; + && method.getAnnotationConsideringFieldAndGetter( + JsonProperty.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) == null; } /** @@ -329,7 +344,10 @@ protected boolean shouldIgnoreMethod(MethodScope method) { * @return whether the field should be in the "required" list or not */ protected boolean getRequiredCheckBasedOnJsonPropertyAnnotation(MemberScope member) { - JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetterIfSupported(JsonProperty.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER) ; + JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetterIfSupported( + JsonProperty.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); return jsonProperty != null && jsonProperty.required(); } @@ -340,7 +358,10 @@ protected boolean getRequiredCheckBasedOnJsonPropertyAnnotation(MemberScope member) { - JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetter(JsonProperty.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetter( + JsonProperty.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); return jsonProperty != null && jsonProperty.access() == JsonProperty.Access.READ_ONLY; } @@ -351,7 +372,10 @@ protected boolean getReadOnlyCheck(MemberScope member) { * @return whether the field should be marked as write-only */ protected boolean getWriteOnlyCheck(MemberScope member) { - JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetter(JsonProperty.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonProperty jsonProperty = member.getAnnotationConsideringFieldAndGetter( + JsonProperty.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); return jsonProperty != null && jsonProperty.access() == JsonProperty.Access.WRITE_ONLY; } } diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonIdentityReferenceDefinitionProvider.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonIdentityReferenceDefinitionProvider.java index 1b79991d..2991b729 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonIdentityReferenceDefinitionProvider.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonIdentityReferenceDefinitionProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 VicTools. + * Copyright 2024 VicTools. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,6 @@ import com.github.victools.jsonschema.generator.MemberScope; import com.github.victools.jsonschema.generator.SchemaGenerationContext; import com.github.victools.jsonschema.generator.TypeContext; - import java.util.Arrays; import java.util.Optional; import java.util.stream.Stream; @@ -88,9 +87,15 @@ public Optional getIdentityReferenceType(ResolvedType javaType, Ty * @return designated type of the applicable identity reference (may be empty) */ public Optional getIdentityReferenceType(MemberScope scope) { - JsonIdentityReference referenceAnnotation = scope.getContainerItemAnnotationConsideringFieldAndGetterIfSupported(JsonIdentityReference.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonIdentityReference referenceAnnotation = scope.getContainerItemAnnotationConsideringFieldAndGetterIfSupported( + JsonIdentityReference.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (referenceAnnotation == null) { - referenceAnnotation = scope.getAnnotationConsideringFieldAndGetter(JsonIdentityReference.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + referenceAnnotation = scope.getAnnotationConsideringFieldAndGetter( + JsonIdentityReference.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); } return this.getIdentityReferenceType(referenceAnnotation, scope.getType(), scope.getContext()); } @@ -112,12 +117,20 @@ private Optional getIdentityReferenceType(JsonIdentityReference re return Optional.empty(); } // additionally, the type itself must have a @JsonIdentityInfo annotation - ResolvedType typeWithIdentityInfoAnnotation = typeContext.getTypeWithAnnotation(javaType, JsonIdentityInfo.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + ResolvedType typeWithIdentityInfoAnnotation = typeContext.getTypeWithAnnotation( + javaType, + JsonIdentityInfo.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (typeWithIdentityInfoAnnotation == null) { // otherwise, the @JsonIdentityReference annotation is simply ignored return Optional.empty(); } - JsonIdentityInfo identityInfoAnnotation = typeContext.getAnnotationFromList(JsonIdentityInfo.class, Arrays.asList(typeWithIdentityInfoAnnotation.getErasedType().getAnnotations()), JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonIdentityInfo identityInfoAnnotation = typeContext.getAnnotationFromList( + JsonIdentityInfo.class, + Arrays.asList(typeWithIdentityInfoAnnotation.getErasedType().getAnnotations()), + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); // @JsonIdentityInfo annotation declares generator with specific identity type ResolvedType identityTypeFromGenerator = typeContext.getTypeParameterFor(typeContext.resolve(identityInfoAnnotation.generator()), ObjectIdGenerator.class, 0); diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java index 385fc4cf..31178e7b 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 VicTools. + * Copyright 2024 VicTools. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonSubTypesResolver.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonSubTypesResolver.java index 652e45e3..74f5ad13 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonSubTypesResolver.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonSubTypesResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 VicTools. + * Copyright 2024 VicTools. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,6 @@ import com.github.victools.jsonschema.generator.TypeContext; import com.github.victools.jsonschema.generator.TypeScope; import com.github.victools.jsonschema.generator.impl.AttributeCollector; - import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.Collection; @@ -126,7 +125,10 @@ public List findTargetTypeOverrides(MemberScope property) { if (this.skipSubtypeResolution(property)) { return null; } - JsonSubTypes subtypesAnnotation = property.getAnnotationConsideringFieldAndGetter(JsonSubTypes.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonSubTypes subtypesAnnotation = property.getAnnotationConsideringFieldAndGetter( + JsonSubTypes.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); return this.lookUpSubtypesFromAnnotation(property.getType(), subtypesAnnotation, property.getContext()); } @@ -174,7 +176,11 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, Sch return null; } final TypeContext typeContext = context.getTypeContext(); - ResolvedType typeWithTypeInfo = typeContext.getTypeWithAnnotation(javaType, JsonTypeInfo.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + ResolvedType typeWithTypeInfo = typeContext.getTypeWithAnnotation( + javaType, + JsonTypeInfo.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (typeWithTypeInfo == null || JacksonHelper.resolveAnnotation(javaType.getErasedType(), JsonSubTypes.class).isPresent() || this.skipSubtypeResolution(javaType, typeContext)) { // no @JsonTypeInfo annotation found or the given javaType is the super type, that should be replaced @@ -182,8 +188,16 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, Sch } Class erasedTypeWithTypeInfo = typeWithTypeInfo.getErasedType(); final List annotationsList = Arrays.asList(erasedTypeWithTypeInfo.getAnnotations()); - JsonTypeInfo typeInfoAnnotation = typeContext.getAnnotationFromList(JsonTypeInfo.class, annotationsList, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); - JsonSubTypes subTypesAnnotation = typeContext.getAnnotationFromList(JsonSubTypes.class, annotationsList, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonTypeInfo typeInfoAnnotation = typeContext.getAnnotationFromList( + JsonTypeInfo.class, + annotationsList, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); + JsonSubTypes subTypesAnnotation = typeContext.getAnnotationFromList( + JsonSubTypes.class, + annotationsList, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); TypeScope scope = typeContext.createTypeScope(javaType); ObjectNode definition = this.createSubtypeDefinition(scope, typeInfoAnnotation, subTypesAnnotation, context); if (definition == null) { @@ -203,7 +217,10 @@ public CustomPropertyDefinition provideCustomPropertySchemaDefinition(MemberScop if (this.skipSubtypeResolution(scope) || JacksonHelper.resolveAnnotation(scope.getType().getErasedType(), JsonSubTypes.class).isPresent()) { return null; } - JsonTypeInfo typeInfoAnnotation = scope.getAnnotationConsideringFieldAndGetter(JsonTypeInfo.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonTypeInfo typeInfoAnnotation = scope.getAnnotationConsideringFieldAndGetter( + JsonTypeInfo.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); if (typeInfoAnnotation == null) { // the normal per-type behaviour is not being overridden, i.e., no need for an inline custom property schema return null; @@ -215,7 +232,10 @@ public CustomPropertyDefinition provideCustomPropertySchemaDefinition(MemberScop .add(context.createStandardDefinitionReference(scope.getType(), this)); return new CustomPropertyDefinition(definition, CustomDefinition.AttributeInclusion.YES); } - JsonSubTypes subTypesAnnotation = scope.getAnnotationConsideringFieldAndGetter(JsonSubTypes.class, JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER); + JsonSubTypes subTypesAnnotation = scope.getAnnotationConsideringFieldAndGetter( + JsonSubTypes.class, + JacksonHelper.JACKSON_ANNOTATIONS_INSIDE_ANNOTATED_FILTER + ); ObjectNode definition = this.createSubtypeDefinition(scope, typeInfoAnnotation, subTypesAnnotation, context); if (definition == null) { return null; diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java index a2eb86c5..671e9dcb 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonUnwrappedDefinitionProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 VicTools. + * Copyright 2024 VicTools. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,6 @@ import com.github.victools.jsonschema.generator.CustomDefinitionProviderV2; import com.github.victools.jsonschema.generator.SchemaGenerationContext; import com.github.victools.jsonschema.generator.SchemaKeyword; - import java.util.ArrayList; import java.util.Arrays; import java.util.List;