Regular expression in OAS validation not properly handled
search cancel

Regular expression in OAS validation not properly handled

book

Article ID: 425471

calendar_today

Updated On:

Products

CA API Gateway

Issue/Introduction

While trying to perform a PUT command using a JSON body, the "Validate Against OpenAPI Document" assertion is throwing a validation error:

PAYLOAD:
{
  "addressLine1": "123 Maple Lane",
  "addressLine2": "<encoded>",
  "city": "<encoded>",
  "state": "<encoded>",
  "postalCode": "<encoded>",
  "country": "<encoded>",
  "phoneNumber": "<encoded>",
  "phoneNumberType": "<encoded>"
}

ERROR: [Path '/addressLine1'] ECMA 262 regex '^[^\p{C}]+$' does not match input string '123 Maple Lane'

It appears the assertion is using pattern ^[^\\p{C}]+$ instead of the defined ^[^\p{C}]+$ in the OAS document. 

In other words, the validator is not appropriately handling the \ JSON escape character.

Environment

API Gateway 11.1

Cause

There is an issue with the OpenAPI validator we are using. The swagger-request-validator-core library uses com.github.fge:json-schema-validator, which validates patterns using Java's java.util.regex.Pattern. This is causing different interpretations of the regex patterns, resulting in unexpected validation failures.

Resolution

For now, the following are the workarounds when updating your spec:

1. Use Java's \p{Cntrl} pattern: '^[^\p{Cntrl}]+$'

or

2. Use ASCII control character range pattern: '^[^\x00-\x1F\x7F]+$'