Skip to content

Commit

Permalink
feat: support rule param.http.type for RequestParam (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Jul 6, 2020
1 parent 1af9b40 commit 8624e04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ object ClassExportRuleKeys {
)

/**
* The type of the param without annotations(@RequestBody/@ModelAttribute/...).
* The type of the param in http request.
* Param with annotation like @RequestBody/@ModelAttribute/... do not compute this rule.
* should return body/form/query
*/
val PARAM_WITHOUT_ANN_TYPE: RuleKey<String> = SimpleRuleKey(
"param.without.ann.type", StringRule::class,
val PARAM_HTTP_TYPE: RuleKey<String> = SimpleRuleKey(
"param.http.type", StringRule::class,
StringRuleMode.SINGLE
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiParameter
import com.itangcent.common.constant.HttpMethod
import com.itangcent.common.model.Header
import com.itangcent.common.model.Request
import com.itangcent.common.model.URL
import com.itangcent.common.model.canHasForm
import com.itangcent.common.model.*
import com.itangcent.common.utils.*
import com.itangcent.idea.plugin.api.export.rule.RequestRuleWrap
import com.itangcent.idea.plugin.utils.SpringClassName
Expand Down Expand Up @@ -64,14 +61,14 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() {

//ModelAttr(form)
if (isModelAttr(param.psi())) {
if (request.method == HttpMethod.NO_METHOD) {
requestHelper!!.setMethod(request,
ruleComputer!!.computer(ClassExportRuleKeys.METHOD_DEFAULT_HTTP_METHOD, param.containMethod())
?: HttpMethod.POST)
}
if (request.method == HttpMethod.GET) {
addParamAsQuery(param, typeObject, request)
} else {
if (request.method == HttpMethod.NO_METHOD) {
requestHelper!!.setMethod(request,
ruleComputer!!.computer(ClassExportRuleKeys.METHOD_DEFAULT_HTTP_METHOD, param.containMethod())
?: HttpMethod.POST)
}
addParamAsForm(param, request, typeObject, paramDesc)
}
return
Expand Down Expand Up @@ -129,6 +126,9 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() {
return
}

//form/body/query
var paramType: String? = null

var paramName: String? = null
var required = false
var defaultVal: Any? = null
Expand All @@ -146,6 +146,10 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() {
|| defaultVal == SpringClassName.REQUEST_HEADER_DEFAULT_NONE) {
defaultVal = ""
}

if (request.method == "GET") {
paramType = "query"
}
}

val readParamDefaultValue = readParamDefaultValue(param)
Expand All @@ -162,22 +166,16 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() {
paramName = param.name()
}

if (defaultVal != null) {
requestHelper!!.addParam(request,
paramName
, defaultVal.toString()
, required
, ultimateComment)
return
}

if (request.method == HttpMethod.GET) {
addParamAsQuery(param, typeObject, request, ultimateComment)
return
}

val paramType = ruleComputer!!.computer(ClassExportRuleKeys.PARAM_WITHOUT_ANN_TYPE,
param)
if (paramType.isNullOrBlank()) {
paramType = ruleComputer!!.computer(ClassExportRuleKeys.PARAM_HTTP_TYPE,
param)
}

if (paramType.notNullOrBlank()) {
when (paramType) {
"body" -> {
Expand All @@ -200,12 +198,20 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() {
}
}


if (typeObject.hasFile()) {
addParamAsForm(param, request, typeObject, ultimateComment)
return
}

if (defaultVal != null) {
requestHelper!!.addParam(request,
paramName
, defaultVal.toString()
, required
, ultimateComment)
return
}

if (request.canHasForm()) {
addParamAsForm(param, request, typeObject, ultimateComment)
return
Expand Down

0 comments on commit 8624e04

Please sign in to comment.