Skip to content

Commit

Permalink
fix: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SLdragon committed Sep 21, 2023
1 parent af5c87f commit eb0b4a7
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions packages/fx-core/src/common/spec-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export function checkParameters(paramObject: OpenAPIV3.ParameterObject[]): Check
for (let i = 0; i < paramObject.length; i++) {
const param = paramObject[i];
const schema = param.schema as OpenAPIV3.SchemaObject;
const isRequiredWithoutDefault = param.required && schema.default === undefined;

if (param.in === "header" || param.in === "cookie") {
if (isRequiredDefaultParam(param, schema)) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
continue;
Expand All @@ -58,14 +59,14 @@ export function checkParameters(paramObject: OpenAPIV3.ParameterObject[]): Check
schema.type !== "number" &&
schema.type !== "integer"
) {
if (isRequiredDefaultParam(param, schema)) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
continue;
}

if (param.in === "query" || param.in === "path") {
if (isRequiredDefaultParam(param, schema)) {
if (isRequiredWithoutDefault) {
paramResult.requiredNum = paramResult.requiredNum + 1;
} else {
paramResult.optionalNum = paramResult.optionalNum + 1;
Expand All @@ -76,13 +77,6 @@ export function checkParameters(paramObject: OpenAPIV3.ParameterObject[]): Check
return paramResult;
}

export function isRequiredDefaultParam(
param: OpenAPIV3.ParameterObject,
schema: OpenAPIV3.SchemaObject
): boolean | undefined {
return param.required && schema.default === undefined;
}

export function checkPostBody(
schema: OpenAPIV3.SchemaObject,
isRequired = false
Expand All @@ -97,13 +91,15 @@ export function checkPostBody(
return paramResult;
}

const isRequiredWithoutDefault = isRequired && schema.default === undefined;

if (
schema.type === "string" ||
schema.type === "integer" ||
schema.type === "boolean" ||
schema.type === "number"
) {
if (isRequired && schema.default === undefined) {
if (isRequiredWithoutDefault) {
paramResult.requiredNum = paramResult.requiredNum + 1;
} else {
paramResult.optionalNum = paramResult.optionalNum + 1;
Expand All @@ -121,7 +117,7 @@ export function checkPostBody(
paramResult.isValid = paramResult.isValid && result.isValid;
}
} else {
if (isRequired && schema.default === undefined) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
}
Expand Down

0 comments on commit eb0b4a7

Please sign in to comment.