From 2dffc16454f3aaa0f686c5b9467523e6f3606e3e Mon Sep 17 00:00:00 2001 From: tangcent Date: Sun, 22 Oct 2023 09:24:03 +0800 Subject: [PATCH] feat: Add recommended configuration for Jackson JsonPropertyOrder --- .../main/resources/.recommend.easy.api.config | 54 ++++++++++++++++- .../helper/RecommendConfigLoaderTest.kt | 58 ++++++++++++++++++- 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/idea-plugin/src/main/resources/.recommend.easy.api.config b/idea-plugin/src/main/resources/.recommend.easy.api.config index 78862d89..971be49d 100644 --- a/idea-plugin/src/main/resources/.recommend.easy.api.config +++ b/idea-plugin/src/main/resources/.recommend.easy.api.config @@ -37,6 +37,46 @@ field.mock[@com.fasterxml.jackson.annotation.JsonFormat]=groovy:``` return "@datetime(\"" + it.ann("com.fasterxml.jackson.annotation.JsonFormat","pattern") + "\")" ``` +#[Jackson_JsonPropertyOrder] +#Support for Jackson annotation JsonPropertyOrder +field.order=@com.fasterxml.jackson.annotation.JsonProperty#index +json.class.parse.before[@com.fasterxml.jackson.annotation.JsonPropertyOrder]=groovy:``` + def properties = it.annValue("com.fasterxml.jackson.annotation.JsonPropertyOrder") + properties.each { property -> + session.push("JsonPropertyOrder-properties-"+it.name(), property) + } + session.set("JsonPropertyOrder-alphabetic"+it.name(), + it.annValue("com.fasterxml.jackson.annotation.JsonPropertyOrder","alphabetic") ?: false) +``` +json.class.parse.after[@com.fasterxml.jackson.annotation.JsonPropertyOrder]=groovy:``` + session.remove("JsonPropertyOrder-properties-"+it.name()) + session.remove("JsonPropertyOrder-"+it.name()) +``` +field.order=groovy:``` + def index = session.get("JsonPropertyOrder-properties-"+ it.containingClass().name())?.indexOf(it.name()) + if (index == -1) { + return null + }else{ + return index + } +``` +field.order.with=groovy:``` + def orderedProperties = session.get("JsonPropertyOrder-properties-"+ a.containingClass().name()) + if(a.name() in orderedProperties) { + if(b.name() in orderedProperties){ + return orderedProperties.indexOf(a.name()) - orderedProperties.indexOf(b.name()) + }else{ + return -1 + } + } else if(b.name() in orderedProperties){ + return 1 + } else if(session.get("JsonPropertyOrder-alphabetic"+a.containingClass().name())){ + return a.name().compareTo(b.name()) + } else { + return null + } +``` + #[Jackson_JsonIgnoreProperties] #Support for Jackson annotation JsonIgnoreProperties @@ -689,10 +729,22 @@ field.order.with=groovy:``` def aDefineClass = a.defineClass() def bDefineClass = b.defineClass() if(aDefineClass==bDefineClass){ - return 0 + return null }else if(aDefineClass.isExtend(bDefineClass.name())){ return 1 }else{ return -1 } +``` + +#[field_order_alphabetically] +# fields alphabetically ordered +field.order.with=groovy:``` + return a.name().compareTo(b.name()) +``` + +#[field_order_alphabetically_descending] +# fields descending alphabetically ordered +field.order.with=groovy:``` + return -a.name().compareTo(b.name()) ``` \ No newline at end of file diff --git a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/settings/helper/RecommendConfigLoaderTest.kt b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/settings/helper/RecommendConfigLoaderTest.kt index 68ae9d6f..1d8b8c81 100644 --- a/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/settings/helper/RecommendConfigLoaderTest.kt +++ b/idea-plugin/src/test/kotlin/com/itangcent/idea/plugin/settings/helper/RecommendConfigLoaderTest.kt @@ -2,6 +2,7 @@ package com.itangcent.idea.plugin.settings.helper import com.itangcent.mock.toUnixString import com.itangcent.test.ResultLoader +import org.junit.jupiter.api.Assertions.assertArrayEquals import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -79,9 +80,60 @@ internal class RecommendConfigLoaderTest { @Test fun testCodes() { - assertEquals( - "[module, ignore, deprecated_java, deprecated_kotlin, not_ignore_irregular_api_method, Jackson, Jackson_JsonIgnoreProperties, Jackson_JsonUnwrapped, Gson, ignore_transient_field, converts, spring_Entity, spring_webflux, spring.validations, spring.ui, jakarta.validation, jakarta.validation(strict), javax.validation, javax.validation(strict), is_file, yapi_tag, yapi_tag_kotlin, yapi_status, yapi_mock, yapi_tag, import_spring_properties, resolve_spring_properties, ignore_serialVersionUID, support_mock_for_general, private_protected_field_only, support_mock_for_javax_validation, not_ignore_static_final_field, Jackson_JsonNaming, Jackson_UpperCamelCaseStrategy, Jackson_SnakeCaseStrategy, Jackson_LowerCaseStrategy, Jackson_KebabCaseStrategy, Jackson_LowerDotCaseStrategy, properties, Fastjson, enum_auto_select_field_by_type, enum_use_name, enum_use_ordinal, ignore_some_common_classes, field_order, field_order_child_first, field_order_parent_first]", - RecommendConfigLoader.codes().contentToString() + assertArrayEquals( + arrayOf( + "module", + "ignore", + "deprecated_java", + "deprecated_kotlin", + "not_ignore_irregular_api_method", + "Jackson", + "Jackson_JsonPropertyOrder", + "Jackson_JsonIgnoreProperties", + "Jackson_JsonUnwrapped", + "Gson", + "ignore_transient_field", + "converts", + "spring_Entity", + "spring_webflux", + "spring.validations", + "spring.ui", + "jakarta.validation", + "jakarta.validation(strict)", + "javax.validation", + "javax.validation(strict)", + "is_file", + "yapi_tag", + "yapi_tag_kotlin", + "yapi_status", + "yapi_mock", + "yapi_tag", + "import_spring_properties", + "resolve_spring_properties", + "ignore_serialVersionUID", + "support_mock_for_general", + "private_protected_field_only", + "support_mock_for_javax_validation", + "not_ignore_static_final_field", + "Jackson_JsonNaming", + "Jackson_UpperCamelCaseStrategy", + "Jackson_SnakeCaseStrategy", + "Jackson_LowerCaseStrategy", + "Jackson_KebabCaseStrategy", + "Jackson_LowerDotCaseStrategy", + "properties", + "Fastjson", + "enum_auto_select_field_by_type", + "enum_use_name", + "enum_use_ordinal", + "ignore_some_common_classes", + "field_order", + "field_order_child_first", + "field_order_parent_first", + "field_order_alphabetically", + "field_order_alphabetically_descending" + ), + RecommendConfigLoader.codes() ) }