nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: glennjones/hapi-swagger
Pull request #653 (2020-03-03 19:49:04)

Fix pattern generation to avoid truncating regex at internal "/"
Fixes https://github.com/glennjones/hapi-swagger/issues/652

I had to `--no-verify` when committing because the code coverage threshold did not pass when the testsuite was limited to just the single file that was updated (lib/properties.js), which is how it runs from within `lint-staged`, however the overall coverage percentage (when run directly as `npm test`) is still showing 98.05%.


Patch:
diff --git a/lib/properties.js b/lib/properties.js
index 7f22a4c..7c267ff 100644
--- a/lib/properties.js
+++ b/lib/properties.js
@@ -286,7 +286,10 @@ internals.properties.prototype.parseString = function(property, joiObj) {
   joiObj._rules.forEach(test => {
     if (Utilities.isObject(test.args) && test.args.regex) {
       if (Utilities.isRegex(test.args.regex)) {
-        property.pattern = test.args.regex.toString().split('/')[1]
+        // get the regex source (as opposed to regex.toString()) so
+        // we exclude the surrounding '/' delimeters as well as any
+        // trailing flags (g, i, m)
+        property.pattern = test.args.regex.source;
       } else {
         property.pattern = test.args.regex;
       }


Test patch:
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index d8681ea..5fe2f0c 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -198,6 +198,16 @@ lab.experiment('property - ', () => {
       type: 'string',
       pattern: '^[a-zA-Z0-9]{3,30}'
     });
+    // covers https://github.com/glennjones/hapi-swagger/issues/652 -
+    // make sure we aren't truncating the regex after an internal '/',
+    // and make sure we omit any regex flags (g, i, m) from the
+    // resulting pattern
+    expect(
+      propertiesNoAlt.parseProperty('x', Joi.string().regex(/^https:\/\/test.com/mi), null, 'body', true, false)
+    ).to.equal({
+      type: 'string',
+      pattern: '^https:\\/\\/test.com'
+    });
 
     expect(propertiesAlt.parseProperty('x', Joi.string().length(0, 'utf8'), null, 'body', true, false)).to.equal({
       type: 'string',
Repository: glennjones/hapi-swagger
Pull request #663 (2020-05-13 13:32:43)

try handling pattern if object is empty


Patch:
diff --git a/lib/properties.js b/lib/properties.js
index 7c267ff..37cc225 100644
--- a/lib/properties.js
+++ b/lib/properties.js
@@ -145,6 +145,15 @@ internals.properties.prototype.parseProperty = function(name, joiObj, parent, pa
           $ref: this.getDefinitionRef(isAlt) + refName
         };
       }
+    } else if (joiObj.$_terms.patterns) {
+      let objectPattern = joiObj.$_terms.patterns[0];
+      let patternSchema = objectPattern.schema;
+      let patternName = patternSchema.$_terms.examples
+      && patternSchema.$_terms.examples[0] ? patternSchema.$_terms.examples[0] : patternSchema.type;
+      property.name = name;
+      property.properties = {
+        [patternName]: this.parseProperty(patternName, objectPattern.rule, property, parameterType, useDefinitions, isAlt),
+      };
     } else {
       // default empty object
       property.properties = {};


Test patch:
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index 5fe2f0c..66a00b1 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -590,6 +590,50 @@ lab.test('parse type object', () => {
         type: 'string'
       }
     }
+  });  expect(propertiesNoAlt.parseProperty('x', Joi.object({ a: Joi.string() }), null, 'body', false, false)).to.equal({
+    name: 'x',
+    type: 'object',
+    properties: {
+      a: {
+        type: 'string'
+      }
+    }
+  });
+  // test without pattern schema example
+  expect(propertiesNoAlt.parseProperty('x', Joi.object().pattern(Joi.string(), Joi.object({
+    y: Joi.string().example('b'),
+  })), null, 'body', false, false)).to.equal({
+    name: 'x',
+    type: 'object',
+    properties: {
+      string: {
+        name: 'string',
+        type: 'object',
+        properties: {
+          'y': {
+            type: 'string'
+          }
+        }
+      }
+    }
+  });
+  // test with pattern schema example
+  expect(propertiesNoAlt.parseProperty('x', Joi.object().pattern(Joi.string().example('a'), Joi.object({
+    y: Joi.string().example('b'),
+  })), null, 'body', false, false)).to.equal({
+    name: 'x',
+    type: 'object',
+    properties: {
+      a: {
+        name: 'a',
+        type: 'object',
+        properties: {
+          'y': {
+            type: 'string'
+          }
+        }
+      }
+    }
   });
 });
Repository: glennjones/hapi-swagger
Pull request #664 (2020-05-26 09:44:44)

handle regex as pattern schema
- added a check to avoid 'undefined of' when using regex as property pattern
- added unit test to cover regex as object property pattern


Patch:
diff --git a/lib/properties.js b/lib/properties.js
index 37cc225..4d22160 100644
--- a/lib/properties.js
+++ b/lib/properties.js
@@ -147,9 +147,11 @@ internals.properties.prototype.parseProperty = function(name, joiObj, parent, pa
       }
     } else if (joiObj.$_terms.patterns) {
       let objectPattern = joiObj.$_terms.patterns[0];
-      let patternSchema = objectPattern.schema;
-      let patternName = patternSchema.$_terms.examples
-      && patternSchema.$_terms.examples[0] ? patternSchema.$_terms.examples[0] : patternSchema.type;
+      let patternName = 'string';
+      if (objectPattern.schema) {
+        patternName = objectPattern.schema.$_terms.examples
+        && objectPattern.schema.$_terms.examples[0] ? objectPattern.schema.$_terms.examples[0] : objectPattern.schema.type;
+      }
       property.name = name;
       property.properties = {
         [patternName]: this.parseProperty(patternName, objectPattern.rule, property, parameterType, useDefinitions, isAlt),


Test patch:
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index 66a00b1..8629cae 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -590,7 +590,8 @@ lab.test('parse type object', () => {
         type: 'string'
       }
     }
-  });  expect(propertiesNoAlt.parseProperty('x', Joi.object({ a: Joi.string() }), null, 'body', false, false)).to.equal({
+  });
+  expect(propertiesNoAlt.parseProperty('x', Joi.object({ a: Joi.string() }), null, 'body', false, false)).to.equal({
     name: 'x',
     type: 'object',
     properties: {
@@ -601,7 +602,7 @@ lab.test('parse type object', () => {
   });
   // test without pattern schema example
   expect(propertiesNoAlt.parseProperty('x', Joi.object().pattern(Joi.string(), Joi.object({
-    y: Joi.string().example('b'),
+    y: Joi.string()
   })), null, 'body', false, false)).to.equal({
     name: 'x',
     type: 'object',
@@ -619,7 +620,7 @@ lab.test('parse type object', () => {
   });
   // test with pattern schema example
   expect(propertiesNoAlt.parseProperty('x', Joi.object().pattern(Joi.string().example('a'), Joi.object({
-    y: Joi.string().example('b'),
+    y: Joi.string()
   })), null, 'body', false, false)).to.equal({
     name: 'x',
     type: 'object',
@@ -635,6 +636,24 @@ lab.test('parse type object', () => {
       }
     }
   });
+  // test with regex as pattern
+  expect(propertiesNoAlt.parseProperty('x', Joi.object().pattern(/\w\d/, Joi.object({
+    y: Joi.string()
+  })), null, 'body', false, false)).to.equal({
+    name: 'x',
+    type: 'object',
+    properties: {
+      string: {
+        name: 'string',
+        type: 'object',
+        properties: {
+          'y': {
+            type: 'string'
+          }
+        }
+      }
+    }
+  });
 });
 
 lab.experiment('property deep - ', () => {