Skip to content

Learned API schema Since v0.2 ​

Most APIs have no published description. With learning on, the positive model also learns the structure of the JSON bodies an endpoint receives and, when asked, of its JSON answers: fields, types, formats, which fields are always present, string lengths, number ranges, array sizes and short value lists. A ready endpoint is offered as a schema suggestion. An approved or hand-written schema is enforced like an imported OpenAPI description, and you can download the schemas as an OpenAPI 3.1 document.

Before you begin ​

  • Role: operator for the application (viewer can read).
  • Learning is on for the application (policy.positive.learning.enabled: true, Request checks > Learned model).
  • Only clean requests teach the model: no module flagged them, the signature score is 0 and the upstream answered below 400.

Turn on body and answer learning ​

text
1. Open Applications > All applications > the application > Request checks > Learned model.
2. In the learning group, keep Learn JSON body structure on.
3. To learn answers too, turn on Learn JSON answers and set Answers learned per second.
4. Select Preview changes, then Apply.
sh
waap apps set -app shop -m "learn bodies" policy.positive.learning.bodies=true policy.positive.learning.responses=true
yaml
# applications[].policy.positive.learning
learning:
  enabled: true
  bodies: true
  responses: true
  response_sample_rate: 1

Answer learning adds latency to sampled answers

Answers are read only when they are JSON, at most 256 KiB, in the response scope and within the sample rate. Those answers wait for their whole body. With answer learning on, the product asks upstreams for uncompressed answers; the data plane compresses towards clients again.

Approve a suggested schema ​

text
1. Open the application's API protection page, Learned schema tab.
2. Under Suggested schemas, select an endpoint to see the evidence and the proposed schema.
3. Select Preview changes, then Apply. Or select Approve all new.
4. To hide a suggestion, select Reject this schema. Select Propose it again to undo it.
sh
waap api learned suggestions -app shop
waap api learned schema -app shop -endpoint "POST /api/orders"
waap api learned approve -app shop -id ID -m "orders schema"
waap api learned reject -app shop -id ID
waap api learned restore -app shop -id ID
yaml
# applications[].policy.api.learned_schemas
- endpoint: POST /api/v1/orders
  source: learned
  request:
    type: object
    required: [item, qty]
    properties:
      item: {type: string, format: uuid}
      qty: {type: integer, minimum: 1, maximum: 100}
      note: {type: string, max_length: 500, nullable: true}
    additional: log
  response:
    status: [200]
    type: object
    properties: {id: {type: string, format: uuid}}

Approving writes an entry with source: learned as a new configuration version. An entry you edit becomes source: manual: relearning never overwrites it and only shows the difference. A rejected suggestion returns when the traffic changes.

Enforce the schemas ​

text
1. On the Learned schema tab, open Settings.
2. Set Learned schema mode, Fields not in the schema and Check answers.
3. Select Preview changes, then Apply.
sh
waap api set -app shop -m "enforce learned schemas" learned.mode=block learned.unknown_fields=refuse
yaml
# applications[].policy.api
learned:
  mode: block
  unknown_fields: refuse
  responses: detect

Start in detect

Nothing is refused until both the application and the feature are in block. A field the schema does not name is only logged unless Fields not in the schema (or the object's additional) says refuse. Answers are only logged, never refused.

When an imported OpenAPI description describes the operation, the description wins and the learned schema is not applied.

Export as OpenAPI ​

text
1. On the Learned schema tab, select Export as OpenAPI.
2. Optionally include the learned but unapproved schemas.
sh
waap api learned openapi -app shop -suggested -o shop-learned.yaml

The document holds one operation per endpoint, path variables as string parameters, the request schema as a JSON request body, and the response schema per listed status (2XX when none is listed). Lengths are in bytes here and in characters in JSON Schema.

What becomes a schema ​

The same thresholds as parameter rules apply (min_samples, min_clients, min_duration, min_confidence).

LearnedRule
typeThe JSON type of at least 99.9% of the non-null values (integer counts as number); a mix gives no type.
nullableA null was seen.
requiredPresent in at least 99.9% of the parent object's occurrences.
formatuuid, email, date-time, hex, base64, integer, decimal, when nearly all values match.
lengthsThe learned range widened by a quarter (at least 1 byte).
value listAt most 32 values, each sent by enough clients and seen at least 10 times.
number rangeAt least 0 when no negative was seen; the largest seen plus a quarter (at least 1).
array sizeThe longest array seen plus a quarter.

A field with too few values or clients is known (never counted as unknown) but not constrained. Values of sensitive fields are never kept, only their shape.

Settings reference ​

Learning ​

SettingWhat it doesDefaultAllowed valuesApplies in
Learn JSON body structure (policy.positive.learning.bodies)Records the structure of clean JSON request bodies.truetrue, falselearning enabled
Learn JSON answers (policy.positive.learning.responses)Also records JSON answers below 400.falsetrue, falselearning enabled
Answers learned per second (policy.positive.learning.response_sample_rate)Answers read per second, per endpoint and node.11 to 100answer learning on

Enforcement ​

SettingWhat it doesDefaultAllowed valuesApplies in
Learned schema mode (learned.mode)Whether violations refuse or only log.empty (same as API protection)detect, block, emptydetect wins over the application's block
Fields not in the schema (learned.unknown_fields)What happens to body fields the schema does not name.loglog, refuseblock mode refuses
Check answers (learned.responses)Checks answers against response schemas.detectoff, detectfindings are only logged

Schema entry ​

SettingWhat it doesDefaultAllowed valuesApplies in
endpointThe endpoint.none (required)METHOD /path/{var}all
sourceWho wrote it.learned when approvedlearned, manualmanual is never overwritten
request, responseThe schema of the body and of the answer.noneschema nodes belowall
additionalWhat happens to unknown fields of an object.the Fields not in the schema settinglog, refuse, allowblock mode refuses
response.statusStatuses the response schema applies to.every 2xx answerstatus listall

Schema nodes: type (object, array, string, integer, number, boolean, null; empty: any), format, nullable; objects: properties, required, additional; arrays: items, max_items; strings: min_length, max_length, enum; numbers: minimum, maximum.

Rules ​

RuleMeaning
api.learned.bodyA value breaks a constraint. The event shows the JSON pointer and the broken constraint.
api.learned.unknown_fieldA body field is not in the schema (for example /admin in a sign-up body: mass assignment).
api.learned.responseAn answer breaks its schema. Always only logged.

One reason per kind and request is reported. Exceptions accept these IDs and api.learned.*.

Limits ​

LimitValue
Depth examined per body16 levels
Array elements examined32
Values examined per body2,048
Field positions per endpoint and direction256 (more are counted as overflow)
Candidate values per field32, each up to 64 bytes
Answer size read256 KiB
Stored observation per endpoint and day256 KiB
Schemas per application2,000 (one per endpoint)
Levels and nodes per endpoint16 and 512 (request and response together)

Troubleshooting ​

No schema is suggested for an endpoint ​

  • Cause: the endpoint has too few clean requests, too few clients or too short a learning time.
  • Check: waap api learned schema -app shop -endpoint "POST /api/orders".
  • Fix: wait for more traffic.

A valid request breaks api.learned.unknown_field ​

  • Cause: the schema was learned before the field existed, or the object overflowed 256 field positions.
  • Fix: edit the schema (it becomes manual) or set additional: allow on that object.