Skip to content

Commit 2bd3f36

Browse files
fix: schema error and test updation flow (hoppscotch#5124)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent 2a63372 commit 2bd3f36

File tree

55 files changed

+1010
-915
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1010
-915
lines changed

packages/hoppscotch-common/src/components/http/Response.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import { HoppRequestDocument } from "~/helpers/rest/document"
2424
import { useResponseBody } from "@composables/lens-actions"
2525
import { getStatusCodeReasonPhrase } from "~/helpers/utils/statusCodes"
2626
import {
27-
HoppRESTResponseOriginalRequest,
2827
HoppRESTRequestResponse,
29-
RESTResOriginalReqSchemaVersion,
28+
HoppRESTResponseOriginalRequest,
29+
makeHoppRESTResponseOriginalRequest,
3030
} from "@hoppscotch/data"
3131
import { editRESTRequest } from "~/newstore/collections"
3232
import { useToast } from "@composables/toast"
@@ -94,17 +94,17 @@ const onSaveAsExample = () => {
9494
requestVariables,
9595
} = response.req
9696
97-
const originalRequest: HoppRESTResponseOriginalRequest = {
98-
v: RESTResOriginalReqSchemaVersion,
99-
method,
100-
endpoint,
101-
headers,
102-
body,
103-
auth,
104-
params,
105-
name,
106-
requestVariables,
107-
}
97+
const originalRequest: HoppRESTResponseOriginalRequest =
98+
makeHoppRESTResponseOriginalRequest({
99+
method,
100+
endpoint,
101+
headers,
102+
body,
103+
auth,
104+
params,
105+
name,
106+
requestVariables,
107+
})
108108
109109
const resName = responseName.value.trim()
110110

packages/hoppscotch-common/src/components/http/TestResultEnv.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</span>
1515
<span class="inline-flex text-secondaryDark">
1616
<icon-lucide-minus class="svg-icons mr-2" />
17-
{{ env.value }}
17+
{{ env.currentValue }}
1818
</span>
1919
<span
2020
v-if="status === 'updations'"
@@ -45,7 +45,7 @@ type Status = "updations" | "additions" | "deletions"
4545
type Props = {
4646
env: {
4747
key: string
48-
value: string
48+
currentValue: string
4949
previousValue?: string
5050
}
5151
status: Status

packages/hoppscotch-common/src/helpers/RequestRunner.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,10 @@ export function runRESTRequest$(
455455
}))
456456
) as E.Right<SandboxTestResult>
457457

458-
const updatedRunResult = updateEnvsAfterTestScript(combinedResult)
459-
460-
tab.value.document.testResults =
461-
translateToSandboxTestResults(updatedRunResult)
458+
tab.value.document.testResults = translateToSandboxTestResults(
459+
combinedResult.right
460+
)
461+
updateEnvsAfterTestScript(combinedResult)
462462
} else {
463463
tab.value.document.testResults = {
464464
description: "",
@@ -492,26 +492,6 @@ export function runRESTRequest$(
492492
}
493493

494494
function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
495-
const updatedGlobalEnvVariables = updateEnvironments(
496-
// @ts-expect-error Typescript can't figure out this inference for some reason
497-
cloneDeep(runResult.right.envs.global),
498-
"global"
499-
)
500-
501-
const updatedSelectedEnvVariables = updateEnvironments(
502-
// @ts-expect-error Typescript can't figure out this inference for some reason
503-
cloneDeep(runResult.right.envs.selected),
504-
"selected"
505-
)
506-
507-
const updatedRunResult = {
508-
...runResult.right,
509-
envs: {
510-
global: updatedGlobalEnvVariables,
511-
selected: updatedSelectedEnvVariables,
512-
},
513-
}
514-
515495
const globalEnvVariables = updateEnvironments(
516496
// @ts-expect-error Typescript can't figure out this inference for some reason
517497
runResult.right.envs.global,
@@ -522,6 +502,11 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
522502
v: 2,
523503
variables: globalEnvVariables,
524504
})
505+
updateEnvironments(
506+
// @ts-expect-error Typescript can't figure out this inference for some reason
507+
cloneDeep(runResult.right.envs.selected),
508+
"selected"
509+
)
525510
if (environmentsStore.value.selectedEnvironmentIndex.type === "MY_ENV") {
526511
const env = getEnvironment({
527512
type: "MY_ENV",
@@ -531,7 +516,7 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
531516
name: env.name,
532517
v: 2,
533518
id: "id" in env ? env.id : "",
534-
variables: updatedRunResult.envs.selected,
519+
variables: runResult.right.envs.selected,
535520
})
536521
} else if (
537522
environmentsStore.value.selectedEnvironmentIndex.type === "TEAM_ENV"
@@ -541,14 +526,12 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
541526
})
542527
pipe(
543528
updateTeamEnvironment(
544-
JSON.stringify(updatedRunResult.envs.selected),
529+
JSON.stringify(runResult.right.envs.selected),
545530
environmentsStore.value.selectedEnvironmentIndex.teamEnvID,
546531
env.name
547532
)
548533
)()
549534
}
550-
551-
return updatedRunResult
552535
}
553536

554537
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
Environment,
3+
translateToNewEnvironmentVariables,
4+
} from "@hoppscotch/data"
5+
6+
/**
7+
* Fixes broken environment versions in the given environments.
8+
* This function ensures that all environment variables are translated
9+
* to the new format, which is necessary for compatibility with the latest
10+
* version of the application.
11+
*
12+
* Some environments may have been created with an unsupported
13+
* variable format, which can lead to issues when trying to access or manipulate those environments.
14+
*
15+
*
16+
* @param envs - The array of environments to fix.
17+
* @returns The fixed array of environments with updated variable formats.
18+
*/
19+
export const fixBrokenEnvironmentVersion = (envs: Environment[]) => {
20+
if (!Array.isArray(envs)) {
21+
return envs
22+
}
23+
return envs.map((env) => ({
24+
...env,
25+
variables: (env.variables ?? []).map(translateToNewEnvironmentVariables),
26+
}))
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
getDefaultRESTRequest,
3+
safelyExtractRESTRequest,
4+
} from "@hoppscotch/data"
5+
import { z } from "zod"
6+
import { REST_TAB_STATE_SCHEMA } from "~/services/persistence/validation-schemas"
7+
8+
type HoppRESTab = z.infer<typeof REST_TAB_STATE_SCHEMA>
9+
10+
/**
11+
* Fixes broken request versions in the given REST tab documents.
12+
* This function ensures that all requests and test runners have valid
13+
* request data, defaulting to the default REST request structure if necessary.
14+
*
15+
* There were requests in the REST tab that had an invalid version
16+
* structure, with response and parent request which could lead to issues when trying to access or
17+
* manipulate those requests. This function iterates through the
18+
* ordered documents of the REST tab and checks each request.
19+
*
20+
* @param docs - The ordered documents of the REST tab to fix.
21+
* @returns The fixed ordered documents with valid request structures.
22+
*/
23+
export const fixBrokenRequestVersion = (
24+
docs: HoppRESTab["orderedDocs"]
25+
): HoppRESTab["orderedDocs"] => {
26+
return docs.map((x: HoppRESTab["orderedDocs"][number]) => {
27+
if (x.doc.type === "request") {
28+
const req = safelyExtractRESTRequest(
29+
x.doc.request,
30+
getDefaultRESTRequest()
31+
)
32+
if (req) {
33+
x.doc.request = req
34+
}
35+
}
36+
37+
if (x.doc.type === "test-runner") {
38+
x.doc.request = safelyExtractRESTRequest(
39+
x.doc.request,
40+
getDefaultRESTRequest()
41+
)
42+
43+
if (x.doc.resultCollection) {
44+
x.doc.resultCollection.requests = x.doc.resultCollection?.requests.map(
45+
(req) => {
46+
return safelyExtractRESTRequest(req, getDefaultRESTRequest())
47+
}
48+
)
49+
}
50+
}
51+
52+
return x
53+
})
54+
}

packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
HoppRESTRequest,
2121
HoppRESTRequestResponses,
2222
HoppRESTResponseOriginalRequest,
23-
RESTResOriginalReqSchemaVersion,
23+
makeHoppRESTResponseOriginalRequest,
2424
} from "@hoppscotch/data"
2525
import { pipe, flow } from "fp-ts/function"
2626
import * as A from "fp-ts/Array"
@@ -828,24 +828,27 @@ const convertPathToHoppReqs = (
828828
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
829829
),
830830

831-
responses: parseOpenAPIResponses(doc, info, {
832-
name: info.operationId ?? info.summary ?? "Untitled Request",
833-
auth: parseOpenAPIAuth(doc, info),
834-
body: parseOpenAPIBody(doc, info),
835-
endpoint,
836-
// We don't need to worry about reference types as the Dereferencing pass should remove them
837-
params: parseOpenAPIParams(
838-
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
839-
),
840-
headers: parseOpenAPIHeaders(
841-
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
842-
),
843-
method: method.toUpperCase(),
844-
requestVariables: parseOpenAPIVariables(
845-
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
846-
),
847-
v: RESTResOriginalReqSchemaVersion,
848-
}),
831+
responses: parseOpenAPIResponses(
832+
doc,
833+
info,
834+
makeHoppRESTResponseOriginalRequest({
835+
name: info.operationId ?? info.summary ?? "Untitled Request",
836+
auth: parseOpenAPIAuth(doc, info),
837+
body: parseOpenAPIBody(doc, info),
838+
endpoint,
839+
// We don't need to worry about reference types as the Dereferencing pass should remove them
840+
params: parseOpenAPIParams(
841+
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
842+
),
843+
headers: parseOpenAPIHeaders(
844+
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
845+
),
846+
method: method.toUpperCase(),
847+
requestVariables: parseOpenAPIVariables(
848+
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
849+
),
850+
})
851+
),
849852
}),
850853
metadata: {
851854
tags: info.tags ?? [],

packages/hoppscotch-common/src/helpers/import-export/import/postman.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import {
1010
knownContentTypes,
1111
makeCollection,
1212
makeRESTRequest,
13-
RESTResOriginalReqSchemaVersion,
1413
ValidContentTypes,
14+
HoppRESTRequestResponses,
15+
makeHoppRESTResponseOriginalRequest,
1516
} from "@hoppscotch/data"
1617
import * as A from "fp-ts/Array"
1718
import { flow, pipe } from "fp-ts/function"
@@ -31,7 +32,6 @@ import {
3132
import { stringArrayJoin } from "~/helpers/functional/array"
3233
import { PMRawLanguage } from "~/types/pm-coll-exts"
3334
import { IMPORTER_INVALID_FILE_FORMAT } from "."
34-
import { HoppRESTRequestResponses } from "@hoppscotch/data"
3535

3636
const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr))
3737

@@ -160,7 +160,7 @@ const getHoppResponses = (
160160
body: response.body ?? "",
161161
headers: getHoppReqHeaders(response.headers),
162162
code: response.code,
163-
originalRequest: {
163+
originalRequest: makeHoppRESTResponseOriginalRequest({
164164
auth: getHoppReqAuth(response.originalRequest?.auth),
165165
body: getHoppReqBody({
166166
body: response.originalRequest?.body,
@@ -178,8 +178,7 @@ const getHoppResponses = (
178178
requestVariables: getHoppReqVariables(
179179
response.originalRequest?.url.variables ?? null
180180
),
181-
v: RESTResOriginalReqSchemaVersion,
182-
},
181+
}),
183182
}
184183
return [response.name, res]
185184
})

packages/hoppscotch-common/src/helpers/teams/TeamEnvironmentAdapter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from "../backend/graphql"
1212
import { TeamEnvironment } from "./TeamEnvironment"
1313
import { Environment, EnvironmentSchemaVersion } from "@hoppscotch/data"
14-
import { entityReference } from "verzod"
1514

1615
type EntityType = "environment"
1716
type EntityID = `${EntityType}-${string}`
@@ -121,18 +120,18 @@ export default class TeamEnvironmentAdapter {
121120
variables: JSON.parse(x.variables),
122121
}
123122

124-
const parsedEnvironment =
125-
entityReference(Environment).safeParse(environment)
123+
const parsedEnvironment = Environment.safeParse(environment)
126124

127125
return <TeamEnvironment>{
128126
id: x.id,
129127
teamID: x.teamID,
130-
environment: parsedEnvironment.success
131-
? parsedEnvironment.data
132-
: {
133-
...environment,
134-
v: EnvironmentSchemaVersion,
135-
},
128+
environment:
129+
parsedEnvironment.type === "ok"
130+
? parsedEnvironment.value
131+
: {
132+
...environment,
133+
v: EnvironmentSchemaVersion,
134+
},
136135
}
137136
})
138137
)

packages/hoppscotch-common/src/helpers/utils/statusCodes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const getFullStatusCodePhrase = () => {
111111
// return all status codes and their phrases
112112
// like code • phrase
113113
export const getStatusCodePhrase = (
114-
code: number | undefined,
114+
code: number | undefined | null,
115115
statusText: string
116116
) => {
117117
if (!code) return statusText

packages/hoppscotch-common/src/kernel/store.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ export const Store = (() => {
5757
return module().listNamespaces(STORE_PATH)
5858
},
5959

60-
listKeys: async (namespace: string): Promise<E.Either<StoreError, string[]>> => {
60+
listKeys: async (
61+
namespace: string
62+
): Promise<E.Either<StoreError, string[]>> => {
6163
return module().listKeys(STORE_PATH, namespace)
6264
},
6365

64-
watch: async (namespace: string, key: string): Promise<StoreEventEmitter<StoreEvents>> => {
66+
watch: async (
67+
namespace: string,
68+
key: string
69+
): Promise<StoreEventEmitter<StoreEvents>> => {
6570
return module().watch(STORE_PATH, namespace, key)
6671
},
6772
} as const

0 commit comments

Comments
 (0)