Skip to content

Commit

Permalink
fix: migrate dsl to account for new ConceptualLocation variant
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapRood committed Mar 24, 2022
1 parent f5d067e commit 4f4acef
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import niceTry from "nice-try";
import {
ChangeType,
ConceptualLocation,
FieldLocation,
IChange,
IFact,
ILocation,
Expand All @@ -25,9 +26,16 @@ import {
OpenApiRequestParameterFact,
OpenApiResponseFact,
OpenAPIV3,
OperationLocation,
PathParameterLocation,
HeaderParameterLocation,
ResponseHeaderLocation,
ResponseLocation,
QueryParameterLocation,
ShouldOrMust,
} from "@useoptic/openapi-utilities";
import { jsonPointerHelpers } from "@useoptic/json-pointer-helpers";
import { RequestLocation } from "@useoptic/openapi-utilities/build/openapi3/sdk/types";

type SnykStablity = "wip" | "experimental" | "beta" | "ga";
type DateString = string; // YYYY-mm-dd
Expand Down Expand Up @@ -85,7 +93,13 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
return this.checks;
}

getContext(location: ILocation): ConceptualLocation & SynkApiCheckContext {
getOperationContext(
location: ILocation,
): OperationLocation & SynkApiCheckContext {
if (!("path" in location.conceptualLocation))
throw new Error(
"Can not get operation context from non-operation location",
);
return {
...location.conceptualLocation,
...this.providedContext,
Expand Down Expand Up @@ -133,15 +147,15 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
selectJsonPath,
...genericEntityRuleImpl<
OpenApiOperationFact,
ConceptualLocation,
OperationLocation,
SynkApiCheckContext,
OpenAPIV3.OperationObject
>(
OpenApiKind.Operation,
this.changelog,
this.nextFacts,
(opFact) => `${opFact.method.toUpperCase()} ${opFact.pathPattern}`,
(location) => this.getContext(location),
(location) => this.getOperationContext(location),
(...items) => this.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(this.nextJsonLike, pointer),
),
Expand Down Expand Up @@ -278,43 +292,49 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
return {
queryParameter: genericEntityRuleImpl<
OpenApiRequestParameterFact,
ConceptualLocation,
QueryParameterLocation,
SynkApiCheckContext,
OpenAPIV3.ParameterObject
>(
OpenApiKind.QueryParameter,
dsl.changelog,
dsl.nextFacts,
(query) => `${query.name}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as QueryParameterLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
),
pathParameter: genericEntityRuleImpl<
OpenApiRequestParameterFact,
ConceptualLocation,
PathParameterLocation,
SynkApiCheckContext,
OpenAPIV3.ParameterObject
>(
OpenApiKind.PathParameter,
dsl.changelog,
dsl.nextFacts,
(path) => `${path.name}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as PathParameterLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
),
header: genericEntityRuleImpl<
OpenApiRequestParameterFact,
ConceptualLocation,
HeaderParameterLocation,
SynkApiCheckContext,
OpenAPIV3.ParameterObject
>(
OpenApiKind.HeaderParameter,
dsl.changelog,
dsl.nextFacts,
(header) => `${header.name}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as HeaderParameterLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
),
Expand All @@ -327,29 +347,33 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
return {
...genericEntityRuleImpl<
OpenApiResponseFact,
ConceptualLocation,
ResponseLocation,
SynkApiCheckContext,
OpenAPIV3.ResponsesObject
>(
OpenApiKind.Response,
dsl.changelog,
dsl.nextFacts,
(response) => `${response.statusCode}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as ResponseLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
),
headers: genericEntityRuleImpl<
OpenApiHeaderFact,
ConceptualLocation,
ResponseHeaderLocation,
SynkApiCheckContext,
OpenAPIV3.HeaderObject
>(
OpenApiKind.ResponseHeader,
dsl.changelog,
dsl.nextFacts,
(header) => `${header.name}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as ResponseHeaderLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
),
Expand Down Expand Up @@ -414,15 +438,17 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
const dsl = this;
return genericEntityRuleImpl<
OpenApiFieldFact,
ConceptualLocation,
FieldLocation,
SynkApiCheckContext,
OpenAPIV3.SchemaObject
>(
OpenApiKind.Field,
dsl.changelog,
dsl.nextFacts,
(field) => `${field.key}`,
(location) => dsl.getContext(location),
(location) =>
dsl.getOperationContext(location) as FieldLocation &
SynkApiCheckContext,
(...items) => dsl.checks.push(...items),
(pointer: string) => jsonPointerHelpers.get(dsl.nextJsonLike, pointer),
);
Expand Down

0 comments on commit 4f4acef

Please sign in to comment.