Generation Errors
Solutions for OpenAPI parsing and code generation issues.
OpenAPI Parsing Errors
Invalid OpenAPI Version
Error: Unsupported OpenAPI version: 2.0Cause: Using Swagger 2.0 instead of OpenAPI 3.0
Solution:
# Convert Swagger 2.0 to OpenAPI 3.0
npx swagger2openapi swagger.json -o openapi.yaml
# Then generate
nxh generate -i openapi.yamlMissing Required Fields
Error: Missing required field: openapiCause: Invalid OpenAPI specification
Solution: Ensure your spec has required fields:
openapi: 3.0.0 # ✅ Required
info: # ✅ Required
title: My API
version: 1.0.0
paths: {} # ✅ RequiredInvalid Schema Reference
Error: Cannot resolve reference: #/components/schemas/InvalidRefCause: Referenced schema doesn't exist
Solution:
# ❌ Bad - Pet schema doesn't exist
paths:
/pets:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Pet' # Error: doesn't exist
# ✅ Good - Define the schema
components:
schemas:
Pet:
type: object
properties:
name:
type: stringCircular References
Error: Circular reference detected: Pet -> Owner -> PetCause: Schema references itself
Solution: Use allOf or break circular reference:
# ✅ Option 1: Break reference
components:
schemas:
Pet:
type: object
properties:
name:
type: string
ownerId: # ✅ Store ID instead
type: number
Owner:
type: object
properties:
name:
type: string
petIds: # ✅ Store IDs instead
type: array
items:
type: number
# ✅ Option 2: Use allOf (if needed)
components:
schemas:
Pet:
type: object
properties:
name:
type: string
owner:
allOf:
- $ref: '#/components/schemas/OwnerSummary'
OwnerSummary: # ✅ Limited version
type: object
properties:
id:
type: number
name:
type: stringPath Parsing Errors
Missing operationId
Warning: Missing operationId for GET /pets, using default: getPetsCause: No operationId defined
Solution: Add operationId to each operation:
# ❌ Bad - no operationId
paths:
/pets/{id}:
get:
summary: Get pet
# ✅ Good - explicit operationId
paths:
/pets/{id}:
get:
operationId: getPetById # ✅ Generates useFetchPetById
summary: Get pet by IDInvalid Path Parameters
Error: Path parameter 'id' not defined in parametersCause: Path has {id} but no parameter definition
Solution:
# ❌ Bad - parameter not defined
paths:
/pets/{id}:
get:
operationId: getPetById
# ✅ Good - parameter defined
paths:
/pets/{id}:
get:
operationId: getPetById
parameters:
- name: id # ✅ Must match path param
in: path
required: true
schema:
type: numberDuplicate operationIds
Error: Duplicate operationId: getPetCause: Multiple operations with same operationId
Solution: Use unique operationIds:
# ❌ Bad - duplicate
paths:
/pets/{id}:
get:
operationId: getPet # ❌ Duplicate
/pets/featured:
get:
operationId: getPet # ❌ Duplicate
# ✅ Good - unique
paths:
/pets/{id}:
get:
operationId: getPetById # ✅ Unique
/pets/featured:
get:
operationId: getFeaturedPets # ✅ UniqueSchema Validation Errors
Invalid Type
Error: Invalid type: unknownCause: Unsupported type in schema
Solution: Use valid OpenAPI types:
# ❌ Bad - invalid types
Pet:
type: object
properties:
id:
type: int # ❌ Should be 'integer'
name:
type: text # ❌ Should be 'string'
# ✅ Good - valid types
Pet:
type: object
properties:
id:
type: integer # ✅ Valid
name:
type: string # ✅ ValidValid types: string, number, integer, boolean, array, object
Missing Response Schema
Warning: No response schema for GET /petsCause: Response has no content schema
Solution:
# ❌ Bad - no schema
paths:
/pets:
get:
responses:
'200':
description: Success
# ✅ Good - with schema
paths:
/pets:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'File System Errors
Output Directory Errors
Error: EACCES: permission denied, mkdir '/generated'Cause: No write permission
Solution:
# Use directory you have permission to write
nxh generate -i swagger.yaml -o ./composables
# Or fix permissions
chmod 755 /path/to/outputFile Already Exists
Error: Output directory not emptyCause: Directory contains files
Solution:
# Clear directory first
rm -rf ./generated/*
# Or use different directory
nxh generate -i swagger.yaml -o ./new-composablesGeneration Warnings
Unused Schemas
Warning: Schema 'OldPet' defined but not usedCause: Schema exists but no operation references it
Solution: Remove unused schemas or ignore warning:
# Remove if truly unused
components:
schemas:
# Pet: # ✅ Remove if not usedDeprecated Operations
Warning: Operation 'getPet' is deprecatedCause: Operation marked as deprecated in spec
Solution:
# Still generates code with deprecation comment
paths:
/pets/{id}:
get:
operationId: getPetById
deprecated: true # ⚠️ Generates with @deprecated commentValidation Tools
Validate OpenAPI Spec
# Using Swagger Editor online
# https://editor.swagger.io/
# Or use CLI validator
npx @apidevtools/swagger-cli validate swagger.yaml
# Or use redocly
npx @redocly/cli lint swagger.yamlCheck Generated Code
# Build TypeScript to check for errors
npm run build
# Run type checker
npm run type-checkCommon Fixes
Fix 1: Ensure Valid OpenAPI 3.0
openapi: 3.0.0 # ✅ Must be 3.0.x
info:
title: My API
version: 1.0.0
paths: {}Fix 2: Add operationIds
# Use this pattern:
# {verb}{Resource}[By{Param}]
GET /pets → getPets
GET /pets/{id} → getPetById
POST /pets → createPet
PUT /pets/{id} → updatePet
DELETE /pets/{id} → deletePetFix 3: Define All Schemas
# Every $ref must have corresponding definition
components:
schemas:
Pet: # ✅ Defined
type: object
Owner: # ✅ Defined
type: objectDebug Tips
1. Start Simple
# Minimal valid spec
openapi: 3.0.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
operationId: getTest
responses:
'200':
description: OK
content:
application/json:
schema:
type: object2. Add Complexity Gradually
- Start with one endpoint
- Add more endpoints
- Add schemas
- Add parameters
- Add request bodies
3. Use Linter
# Install OpenAPI linter
npm install -g @redocly/cli
# Lint your spec
redocly lint swagger.yaml