Skip to content

Add lenient validation mode to handle minor spec violations #5

@lanej

Description

@lanej

Some carrier API specs have minor OpenAPI violations (e.g., pattern on number type, maximum on string type) that other tools like openapi-generator-cli handle gracefully by ignoring invalid constraints. ogen should offer a lenient mode for better real-world compatibility.

Specs Affected

  • carriers/royal_mail/specs/openapi.yaml - maximum/minimum on string type
  • carriers/hermes/specs/openapi.yaml - pattern on number type

Current Errors

openapi.yaml:6124:24 -> maximum: unexpected field for type "string"
	6124 |             "maximum": 1000000,
	6125 |             "minimum": 0.001,
	6127 |             "type": "string",

openapi.yaml:2403:24 -> pattern: unexpected field for type "number"
	2403 |             "pattern": "^\\d+(\\.\\d{1,6})?$",
	2404 |             "type": "number",

Context

These ARE spec errors (OpenAPI spec doesn't allow maximum on strings or pattern on numbers), BUT:

  • Vendor-provided specs often have these issues
  • The constraints are documentation, not enforceable in generated code anyway
  • Other tools (openapi-generator-cli, Postman) silently ignore invalid constraints

Minimal Reproduction

openapi: 3.0.1
info:
  title: Invalid Constraints Test
  version: 1.0.0
paths:
  /product:
    post:
      operationId: createProduct
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                # Error: maximum/minimum on string type
                weight:
                  type: string
                  maximum: 1000
                  minimum: 0.001
                  description: Weight in KG
                # Error: pattern on number type
                quantity:
                  type: number
                  pattern: '^\d+(\.\d{1,2})?$'
                  description: Quantity
      responses:
        '201':
          description: Created

Test: ogen --target /tmp/test --clean test.yaml

Proposed Solution

Add --lenient flag or config option:

ogen --lenient --target ./client --clean spec.yaml
# ogen.yml
generator:
  validation_mode: lenient  # strict (default) | lenient | warn

Behavior:

  • strict (default): Fail on schema validation errors (current behavior)
  • lenient: Ignore invalid constraints, generate code anyway
  • warn: Log warnings but continue generation

Expected

  • Option to ignore/warn about schema validation errors
  • Generate code anyway, treating constraints as documentation

Actual

Generation fails with validation error

Impact

  • Cannot use ogen with real-world vendor-provided specs that have minor violations
  • Forces manual spec editing before generation

Note

While these ARE spec errors, a lenient mode would improve ogen's compatibility with real-world APIs without compromising correctness of generated code (constraints wouldn't be enforced anyway).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions