Repository: pmcelhaney/counterfact
Pull request #192 (2022-09-10 18:54:56)

adds a requirement.get() function for going down one level without escaping
…caping


Patch:
diff --git a/src/typescript-generator/operation-coder.js b/src/typescript-generator/operation-coder.js
index 5b14586..9c0c6cd 100644
--- a/src/typescript-generator/operation-coder.js
+++ b/src/typescript-generator/operation-coder.js
@@ -14,7 +14,7 @@ export class OperationCoder extends Coder {
   }
 
   write() {
-    const responses = this.requirement.select("responses");
+    const responses = this.requirement.get("responses");
 
     const requestProperties = this.requirement.data.parameters
       ? Array.from(
diff --git a/src/typescript-generator/operation-type-coder.js b/src/typescript-generator/operation-type-coder.js
index badd0d6..c2e9f7d 100644
--- a/src/typescript-generator/operation-type-coder.js
+++ b/src/typescript-generator/operation-type-coder.js
@@ -14,7 +14,7 @@ export class OperationTypeCoder extends Coder {
   }
 
   responseTypes(script) {
-    const responses = this.requirement.select("responses");
+    const responses = this.requirement.get("responses");
 
     return Object.entries(responses.data)
       .flatMap(([responseCode, response]) =>
@@ -28,10 +28,10 @@ export class OperationTypeCoder extends Coder {
       )
       .map((type) => {
         const schema = responses
-          .select(type.responseCode)
-          .select("content")
-          ?.select(type.contentType.replace("/", "~1"))
-          ?.select("schema");
+          .get(type.responseCode)
+          .get("content")
+          ?.get(type.contentType)
+          ?.get("schema");
 
         const body = schema
           ? new SchemaTypeCoder(schema).write(script)
@@ -72,7 +72,7 @@ export class OperationTypeCoder extends Coder {
       nodePath.relative(nodePath.dirname(script.path), "context/context.js")
     );
 
-    const parameters = this.requirement.select("parameters");
+    const parameters = this.requirement.get("parameters");
     const queryType =
       parameters === undefined
         ? "undefined"
@@ -97,7 +97,7 @@ export class OperationTypeCoder extends Coder {
       : "undefined";
 
     const responseType = new ResponseTypeCoder(
-      this.requirement.select("responses")
+      this.requirement.get("responses")
     ).write(script);
 
     return `({ query, path, header, body, context, tools }: { query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: Context, response: ${responseType}, tools: ${script.importType(
diff --git a/src/typescript-generator/parameters-type-coder.js b/src/typescript-generator/parameters-type-coder.js
index 4d6b0fb..5bc2478 100644
--- a/src/typescript-generator/parameters-type-coder.js
+++ b/src/typescript-generator/parameters-type-coder.js
@@ -22,14 +22,14 @@ export class ParametersTypeCoder extends Coder {
     const typeDefinitions = this.requirement.data
       .filter((parameter) => parameter.in === this.placement)
       .map((parameter, index) => {
-        const requirement = this.requirement.select(String(index));
+        const requirement = this.requirement.get(String(index));
 
         const { name, required } = parameter;
 
         const optionalFlag = required ? "" : "?";
 
         return `${name}${optionalFlag}: ${new SchemaTypeCoder(
-          requirement.select("schema")
+          requirement.get("schema")
         ).write(script)}`;
       });
 
diff --git a/src/typescript-generator/requirement.js b/src/typescript-generator/requirement.js
index 32aea09..b9cfe07 100644
--- a/src/typescript-generator/requirement.js
+++ b/src/typescript-generator/requirement.js
@@ -17,6 +17,18 @@ export class Requirement {
     return this.specification.requirementAt(this.data.$ref, this.url);
   }
 
+  get(item) {
+    if (!(item in this.data)) {
+      return undefined;
+    }
+
+    return new Requirement(
+      this.data[item],
+      `${this.url}/${item}`,
+      this.specification
+    );
+  }
+
   select(path, data = this.data, basePath = "") {
     const [head, ...tail] = path.split("/");
 
diff --git a/src/typescript-generator/response-type-coder.js b/src/typescript-generator/response-type-coder.js
index 9a5283c..f022353 100644
--- a/src/typescript-generator/response-type-coder.js
+++ b/src/typescript-generator/response-type-coder.js
@@ -28,10 +28,10 @@ export class ResponseTypeCoder extends Coder {
             `"${mediaType}": { 
               schema:  ${new SchemaTypeCoder(
                 this.requirement
-                  .select(statusCode)
-                  .select("content")
-                  .select(mediaType.replace("/", "~1"))
-                  .select("schema")
+                  .get(statusCode)
+                  .get("content")
+                  .get(mediaType)
+                  .get("schema")
               ).write(script)}
             }`
         )
@@ -40,12 +40,12 @@ export class ResponseTypeCoder extends Coder {
   }
 
   buildHeaders(script, responseCode) {
-    const response = this.requirement.select(responseCode);
+    const response = this.requirement.get(responseCode);
 
     const properties = Object.keys(response.data.headers ?? {}).map(
       (name) =>
         `"${name}": { schema: ${new SchemaTypeCoder(
-          response.select("headers").select(name).select("schema")
+          response.get("headers").get(name).get("schema")
         ).write(script)}}`
     );
 
diff --git a/src/typescript-generator/schema-coder.js b/src/typescript-generator/schema-coder.js
index 922483c..58cd100 100644
--- a/src/typescript-generator/schema-coder.js
+++ b/src/typescript-generator/schema-coder.js
@@ -39,9 +39,7 @@ export class SchemaCoder extends Coder {
   arraySchema(script) {
     return `{
         type: "array",     
-        items: ${new SchemaCoder(this.requirement.select("items")).write(
-          script
-        )}
+        items: ${new SchemaCoder(this.requirement.get("items")).write(script)}
       }`;
   }
 
diff --git a/src/typescript-generator/schema-type-coder.js b/src/typescript-generator/schema-type-coder.js
index 49df72f..e8389db 100644
--- a/src/typescript-generator/schema-type-coder.js
+++ b/src/typescript-generator/schema-type-coder.js
@@ -20,7 +20,7 @@ export class SchemaTypeCoder extends Coder {
       return "unknown";
     }
 
-    const requirement = this.requirement.select("additionalProperties");
+    const requirement = this.requirement.get("additionalProperties");
 
     return new SchemaTypeCoder(requirement).write(script);
   }
@@ -29,7 +29,7 @@ export class SchemaTypeCoder extends Coder {
     const { data } = this.requirement;
 
     const properties = Object.keys(data.properties ?? {}).map((name) => {
-      const property = this.requirement.select(`properties/${name}`);
+      const property = this.requirement.get("properties").get(name);
 
       const isRequired =
         data.required?.includes(name) || property.data.required;
@@ -50,7 +50,7 @@ export class SchemaTypeCoder extends Coder {
   }
 
   arraySchema(script) {
-    return `Array<${new SchemaTypeCoder(this.requirement.select("items")).write(
+    return `Array<${new SchemaTypeCoder(this.requirement.get("items")).write(
       script
     )}>`;
   }


Test patch:
diff --git a/test/typescript-generator/requirement.test.js b/test/typescript-generator/requirement.test.js
index 4480b18..fe9536c 100644
--- a/test/typescript-generator/requirement.test.js
+++ b/test/typescript-generator/requirement.test.js
@@ -26,6 +26,16 @@ describe("a Requirement", () => {
     expect(requirement.select("foo~1bar").data).toBe("slash");
   });
 
+  it("get(name) - does not escape special characters", () => {
+    const requirement = new Requirement({
+      "foo/bar": "slash",
+      "foo~bar": "tilde",
+    });
+
+    expect(requirement.get("foo~bar").data).toBe("tilde");
+    expect(requirement.get("foo/bar").data).toBe("slash");
+  });
+
   it("knows if it is a reference ($ref)", () => {
     const yes = new Requirement({
       $ref: "some.yaml#other/url",