Skip to content

Add resource path segment classifier and validation rules#342

Open
mo-alras wants to merge 3 commits intocommercetools:mainfrom
mo-alras:add-resource-path-segment-rules
Open

Add resource path segment classifier and validation rules#342
mo-alras wants to merge 3 commits intocommercetools:mainfrom
mo-alras:add-resource-path-segment-rules

Conversation

@mo-alras
Copy link
Copy Markdown
Contributor

@mo-alras mo-alras commented Mar 25, 2026

Summary

  • Introduces ResourceClassifier that classifies RAML resource path segments into 8 categories (COLLECTION, IDENTIFIED_OBJECT, SCOPING_PREFIX, ACTION_ENDPOINT, MATCHING_ENDPOINT, IDENTIFIER_LOOKUP, SINGLETON, UNKNOWN) using structural signals from the RAML model (resource type annotations, URI patterns, HTTP methods, child resources).
  • Enhances ResourcePluralRule with classifier guard so it only checks COLLECTION resources, eliminating the need for most manual exclusions in ruleset.xml (the HTTP API currently has 29 exclusions, History API has 21).
  • Adds ResourceSingularRule to validate that singleton resources use singular names.
  • Adds ScopingOrderRule to validate that scoping prefixes (as-*in-*me) appear in the correct composition order.
  • Adds ResourceAllowedCharactersRule to validate path segments only contain lowercase letters, digits, and hyphens.
  • Adds ResourceNoFileExtensionRule to validate path segments do not contain file extensions.
  • Externalizes the action verb whitelist to default-action-verbs.txt (13 verbs), extensible per-API via <option type="action-verb"> in ruleset.xml.
  • Adds ACTION_VERB to RuleOptionType enum.
  • Reduces ResourcePluralRule.defaultExcludes from 6 entries to 2 ("", "inventory") — the classifier now auto-handles login, me, import, in-store.

Classification logic (first match wins)

  1. URI literal contains = → IDENTIFIER_LOOKUP
  2. URI is /{variable} → IDENTIFIED_OBJECT
  3. Path starts with as-/in- or equals me → SCOPING_PREFIX
  4. Path starts with matching- → MATCHING_ENDPOINT
  5. Resource type is baseDomain → COLLECTION
  6. Resource type is baseResource → IDENTIFIED_OBJECT
  7. Has child with /{var} or /key={key} → COLLECTION
  8. Path matches action verb whitelist or POST-only leaf → ACTION_ENDPOINT
  9. Has GET, no ID children → SINGLETON
  10. Has non-trivial children → COLLECTION
  11. Otherwise → UNKNOWN

Follow-up: commercetools-docs cleanup

After this PR is merged and released, a follow-up PR in commercetools/commercetools-docs should:

  1. Bump rmf-codegen version in api-specs/*/package.json
  2. Clean up ruleset.xml exclusion lists — most ResourcePluralRule exclusions become unnecessary:
    • api-specs/api: 29 → ~1-2 (keep inventory, verify password)
    • api-specs/history: 21 → ~1
    • api-specs/connect: 1 → 0
    • api-specs/checkout: 2 → 0-1
    • api-specs/insights: 1 → 0
  3. Add new rules to each ruleset.xml:
    <rule><name>com.commercetools.rmf.validators.ResourceSingularRule</name></rule>
    <rule><name>com.commercetools.rmf.validators.ScopingOrderRule</name></rule>
    <rule><name>com.commercetools.rmf.validators.ResourceAllowedCharactersRule</name></rule>
    <rule><name>com.commercetools.rmf.validators.ResourceNoFileExtensionRule</name></rule>

Test plan

  • All 91 existing + new tests pass (./gradlew :ctp-validators:test)
  • Verify against commercetools HTTP API spec after release
  • Verify against History, Connect, Checkout, Insights, Import API specs

🤖 Generated with Claude Code

ResourceAllowedCharactersRule validates that resource path segments only
contain lowercase letters, digits, and hyphens ([a-z0-9-]).

ResourceNoFileExtensionRule validates that resource path segments do not
contain file extensions (e.g. .json, .xml, .csv).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mo-alras mo-alras requested a review from a team as a code owner March 25, 2026 14:09
…ng order rules

Introduces ResourceClassifier that classifies RAML resources into 8 categories
(COLLECTION, IDENTIFIED_OBJECT, SCOPING_PREFIX, ACTION_ENDPOINT, MATCHING_ENDPOINT,
IDENTIFIER_LOOKUP, SINGLETON, UNKNOWN) using structural signals. This enables
ResourcePluralRule to automatically skip non-collection segments instead of
relying on manual exclusion lists. Also adds ResourceSingularRule for singleton
validation, ScopingOrderRule for prefix composition order, and an externalized
action verb whitelist (default-action-verbs.txt) extensible via ruleset.xml.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mo-alras mo-alras changed the title Add ResourceAllowedCharactersRule and ResourceNoFileExtensionRule Add resource path segment classifier and validation rules Mar 31, 2026
@mo-alras mo-alras requested a review from jenschude March 31, 2026 14:51
result.validationResults.size() == 17
}

def "property min max abbreviation rule"() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prompt should be re-run as it deleted older test cases

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants