Skip to content

openapi: decide whether an empty pattern is kept #2065

In progress

Description

@OmarAlJarrah

Problem

A pattern written as the empty string reaches the IR in two ways, and the position decides which. Constraints.Pattern is a string with omitempty, so the IR cannot hold an empty one; the compiler drops it beside one type and keeps it verbatim beside a type list that lowers to a union:

openapi: 3.1.0
info: {title: t, version: "1"}
paths: {}
components:
  schemas:
    S: {type: string, pattern: ""}
    Bounded: {type: string, minLength: 1, pattern: ""}
    P: {pattern: ""}
    U: {type: [string, integer], pattern: ""}
$ morphic compile --diag-limit 0 spec.yaml >ir.json; echo "exit $?"
info openapi/degraded-construct spec.yaml#/components/schemas/U: this position lowered to a node of kind "union", which has no home for pattern declared beside it; kept verbatim under Unmodeled
exit 0
$ jq -c '.types | to_entries[] | select(.key | test("schemas/(S|Bounded|P|U)$")) | {k: (.key | split("/") | last), kind: .value.kind, constraints: .value.constraints, unmodeled: ((.value.unmodeled // {}) | map_values({reason, value}))}' ir.json
{"k":"Bounded","kind":"scalar","constraints":{"minLength":1,"uniqueItems":false},"unmodeled":{}}
{"k":"P","kind":"scalar","constraints":null,"unmodeled":{}}
{"k":"S","kind":"scalar","constraints":null,"unmodeled":{}}
{"k":"U","kind":"union","constraints":null,"unmodeled":{"openapi:pattern":{"reason":"degraded_lowering","value":""}}}

S, Bounded and P carry no trace of the keyword and draw no diagnostic; U keeps it and reports an info. S and U write the same keyword with the same value, and U with pattern: "^a" draws neither the entry nor the report.

What the keyword means: JSON Schema 2020-12 (Validation §6.3.3) takes pattern as a string that "SHOULD be a valid regular expression", and the empty regular expression matches every string, so pattern: "" constrains nothing: dropping it is exact and keeping it records a constraint that is no constraint. Either answer is correct for the instance set; what differs is that one keyword with one value is read two ways.

For comparison, format: "" is the same kind of keyword: on main it is dropped beside one type; with #1996 (at 6630476) S2: {type: string, format: ""} is kept under openapi:format (degraded_lowering) with an info. So the question below is about the whole family of empty-string annotation-like keywords, not pattern alone, and the answer should be one rule for them.

Compare the same question for empty validation-only collections (#2026).

Cause

annotation.Constraints (compilers/openapi/internal/annotation/constraints.go) copies the parser's GetPattern() into Constraints.Pattern, whose empty string is "no pattern". A one-type position then holds nothing for "" and the keyword counts as read. In a type list, a non-empty pattern gives the string variant a Scalar of its own (U: {type: [string, integer], pattern: "^a"} lowers to a union whose string variant is anon/components/schemas/U/type/0 holding pattern: "^a", with no diagnostic); an empty one leaves the variant the bare string primitive, the keyword counts as unread, and the unread-keyword keeper keeps it on the union as written (morphic @9562ea46d).

Direction

A draft; the choice is between two uniform readings.

  1. An empty pattern is the keyword left out, at every position. The union keeps nothing for it, and the rule table in docs/compilers/openapi.md says so beside the other forms that constrain nothing. It costs a consumer that wants to know the author wrote the keyword at all; the IR holds no field for that for any keyword that constrains nothing (required: [], properties: {}).
  2. An empty pattern is kept wherever it is written, under openapi:pattern with an info, as the union does. It costs one entry and one report per occurrence for a keyword that changes no instance's validity.

The first is the smaller change (the union's position drops its entry) and matches what Constraints can say. Whichever is chosen, format: "" follows the same rule, and a test beside the rule pins each position (one type, a type list, untyped, beside a $ref).

Out of scope

Related: #2026, #1996

Priority: P4, rule 3 (off the golden spec; cosmetic). jq '[.. | objects | select(has("pattern") and .pattern == "")] | length' openapi.json → 0 (cloudflare/api-schemas@8b1a4518; morphic @9562ea46d; #1996 at 6630476 gives the same for pattern).

Activity

  1. added
    type:feature
    priority:P4
    on Nov 18, 2026
  2. OmarAlJarrah commented on Nov 30, 2026

    @OmarAlJarrah
    MemberAuthor

    Decided and fixed in #2354 at 7213104, which closes this on merge. An empty pattern matches every string, so it is read as the keyword left out wherever it is written (docs/compilers/openapi.md#null-keywords): U: {type: [string, integer], pattern: ""} now keeps nothing and draws no finding, as S: {type: string, pattern: ""} does. format: "" departs from the Direction and stays kept under openapi:format with its info: format is an annotation whose value is collected (JSON Schema Validation 2020-12 §7.1), so even an empty one says something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:P4type:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions