@@ -29,7 +29,7 @@ interface YapiSaveInterceptor {
2929 * @return return {@code false} [YapiApiHelper] will discard this apiInfo.
3030 * else [YapiApiHelper] will save this apiInfo to yapi server.
3131 */
32- fun beforeSaveApi (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>): Boolean?
32+ fun beforeSaveApi (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>): Boolean?
3333}
3434
3535@Singleton
@@ -44,7 +44,7 @@ class YapiSaveInterceptorCompositeProvider : SpiCompositeBeanProvider<YapiSaveIn
4444 */
4545@ConditionOnSetting(" yapiExportMode" , havingValue = " ALWAYS_UPDATE" )
4646class AlwaysUpdateYapiSaveInterceptor : YapiSaveInterceptor {
47- override fun beforeSaveApi (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>): Boolean {
47+ override fun beforeSaveApi (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>): Boolean {
4848 return true
4949 }
5050}
@@ -57,8 +57,9 @@ val ALWAYS_UPDATE_API_SAVE_INTERCEPTOR = AlwaysUpdateYapiSaveInterceptor()
5757 */
5858@ConditionOnSetting(" yapiExportMode" , havingValue = " NEVER_UPDATE" )
5959class NeverUpdateYapiSaveInterceptor : YapiSaveInterceptor {
60- override fun beforeSaveApi (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>): Boolean {
61- return ! apiHelper.existed(apiInfo)
60+ override fun beforeSaveApi (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>): Boolean {
61+ val yapiApiHelper = actionContext.instance(YapiApiHelper ::class )
62+ return ! yapiApiHelper.existed(apiInfo)
6263 }
6364}
6465
@@ -73,22 +74,24 @@ class AlwaysAskYapiSaveInterceptor : YapiSaveInterceptor {
7374 private var selectedYapiSaveInterceptor: YapiSaveInterceptor ? = null
7475
7576 @Synchronized
76- override fun beforeSaveApi (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>): Boolean? {
77+ override fun beforeSaveApi (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>): Boolean? {
7778 if (selectedYapiSaveInterceptor != null ) {
78- return selectedYapiSaveInterceptor!! .beforeSaveApi(apiHelper , apiInfo)
79+ return selectedYapiSaveInterceptor!! .beforeSaveApi(actionContext , apiInfo)
7980 }
80- if (! apiHelper.existed(apiInfo)) {
81+
82+ val yapiApiHelper = actionContext.instance(YapiApiHelper ::class )
83+ if (! yapiApiHelper.existed(apiInfo)) {
8184 return true
8285 }
86+
8387 val valueHolder = ValueHolder <Boolean >()
84- val context = ActionContext .getContext() ? : return true
85- context.instance(MessagesHelper ::class ).showAskWithApplyAllDialog(
88+ actionContext.instance(MessagesHelper ::class ).showAskWithApplyAllDialog(
8689 " The api [${apiInfo[" title" ]} ] already existed in the project.\n " +
8790 " Do you want update it?" ,
8891 ConfirmationDialogLabels (okText = " Update" , noText = " Skip" , cancelText = " Cancel" )
8992 ) { ret, applyAll ->
9093 if (ret == Messages .CANCEL ) {
91- context .stop()
94+ actionContext .stop()
9295 valueHolder.success(false )
9396 return @showAskWithApplyAllDialog
9497 }
@@ -115,25 +118,27 @@ class AlwaysAskYapiSaveInterceptor : YapiSaveInterceptor {
115118 * Only works when the configuration "yapi.no_update.description" is set to true.
116119 */
117120class NoUpdateDescriptionYapiSaveInterceptor : YapiSaveInterceptor {
118- override fun beforeSaveApi (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>): Boolean? {
119- recoverDescription(apiHelper, apiInfo)
121+ override fun beforeSaveApi (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>): Boolean? {
122+ val noUpdateDescription = actionContext
123+ .instance(ConfigReader ::class )
124+ .first(" yapi.no_update.description" )
125+ ?.toBool(false ) == true
126+
127+ if (noUpdateDescription) {
128+ recoverDescription(actionContext, apiInfo)
129+ }
120130 return null
121131 }
122132
123133 /* *
124134 * Retrieves the existing API information from YAPI and copies the description and markdown
125135 * fields to the new API information to prevent them from being overwritten.
126136 */
127- private fun recoverDescription (apiHelper : YapiApiHelper , apiInfo : HashMap <String , Any ?>) {
128- val disable = ActionContext .getContext()
129- ?.instance(ConfigReader ::class )
130- ?.first(" yapi.no_update.description" )
131- ?.toBool(false ) ? : false
132- if (! disable) return
133-
134- val existedApi = apiHelper.findExistApi(apiInfo) ? : return
137+ private fun recoverDescription (actionContext : ActionContext , apiInfo : HashMap <String , Any ?>) {
138+ val yapiApiHelper = actionContext.instance(YapiApiHelper ::class )
139+ val existedApi = yapiApiHelper.findExistApi(apiInfo) ? : return
135140 val apiId = existedApi.sub(" _id" )?.asString ? : return
136- val existedApiInfo = apiHelper .getApiInfo(apiInfo[" token" ] as String , apiId)
141+ val existedApiInfo = yapiApiHelper .getApiInfo(apiInfo[" token" ] as String , apiId)
137142
138143 val existedDescription = existedApiInfo.sub(" desc" )?.asString ? : " "
139144 apiInfo[" desc" ] = existedDescription
@@ -149,17 +154,17 @@ class NoUpdateDescriptionYapiSaveInterceptor : YapiSaveInterceptor {
149154 */
150155@ConditionOnSetting(" yapiExportMode" , havingValue = " UPDATE_IF_CHANGED" )
151156class UpdateIfChangedYapiSaveInterceptor : YapiSaveInterceptor {
152- @Inject
153- private lateinit var logger : Logger
154-
155- override fun beforeSaveApi ( apiHelper : YapiApiHelper , apiInfo : HashMap < String , Any ?>): Boolean? {
156- if (! apiHelper .existed(apiInfo)) {
157+ override fun beforeSaveApi ( actionContext : ActionContext , apiInfo : HashMap < String , Any ?>): Boolean? {
158+ val yapiApiHelper = actionContext.instance( YapiApiHelper :: class )
159+ val logger = actionContext.instance( Logger :: class )
160+
161+ if (! yapiApiHelper .existed(apiInfo)) {
157162 return true
158163 }
159164
160- val existedApi = apiHelper .findExistApi(apiInfo) ? : return true
165+ val existedApi = yapiApiHelper .findExistApi(apiInfo) ? : return true
161166 val apiId = existedApi.sub(" _id" )?.asString ? : return true
162- val existedApiInfo = apiHelper .getApiInfo(apiInfo[" token" ] as String , apiId) ? : return true
167+ val existedApiInfo = yapiApiHelper .getApiInfo(apiInfo[" token" ] as String , apiId) ? : return true
163168
164169 // Compare key fields that determine if the API has changed
165170 if (isApiUnchanged(apiInfo, existedApiInfo)) {
0 commit comments