datamodel-code-generator
datamodel-code-generator copied to clipboard
Error - A Parser can not resolve classes
Context I am using an Open API specs to serve as the single source of truth in my application across my frontend written in Typescript and my backend written in Python.
Describe the bug
I get a "A Parser can not resolve classes" error if I don't reference some models from my openapi.yaml file. I checked multiple times that there were no issues but couldn't find anything wrong.
To Reproduce
*Create the following files:
./openapi.yaml
./schemas/feedback.yaml
*Run datamodel-codegen --input ./schemas/openapi.yaml --input-file-type openapi --output ../../apps/backend/app/generated/models.py --output-model-type pydantic_v2.BaseModel --use-schema-description --use-field-description --field-constraints
*Get the following error: Exception: A Parser can not resolve classes: [class: openapi.yaml#/components/schemas/FeedbackItem references: frozenset({'schemas/feedback.yaml#/FeedbackItem'})], [class: schemas/feedback.yaml#/FeedbackItem references: frozenset({'schemas/feedback.yaml#/FeedbackItemBase'})], [class: openapi.yaml#/components/schemas/FeedbackItemCreate references: frozenset({'schemas/feedback.yaml#/FeedbackItemCreate'})], [class: schemas/feedback.yaml#/FeedbackItemCreate references: frozenset({'schemas/feedback.yaml#/FeedbackItemBase'})].
*Add FeedbackItemBase to openapi.yaml. Run the command again, it works.
Example schema:
./openapi.yaml
openapi: 3.0.0
info:
title: Workshop Feedback API
version: 1.0.0
description: API for managing workshop feedback and summaries.
contact:
name: test
email: [email protected]
servers:
- url: https://api.yourdomain.com/v1 # Replace with your Production URL
description: Production server
- url: https://staging-api.yourdomain.com/v1 # Replace with your Staging URL
description: Staging server
- url: http://localhost:8080 # Replace with your local development URL/port
description: Local development server
tags:
- name: Feedback Summary
description: Operations related to generating and managing feedback summaries.
paths:
/workshops/{workshopId}/feedbacks/summary:
$ref: "./paths/feedback_path.yaml"
components:
schemas:
Workshop:
$ref: "./schemas/workshops.yaml#/Workshop"
WorkshopCreate:
$ref: "./schemas/workshops.yaml#/WorkshopCreate"
WorkshopBase:
$ref: "./schemas/workshops.yaml#/WorkshopBase"
FeedbackItem:
$ref: "./schemas/feedback.yaml#/FeedbackItem"
FeedbackItemCreate:
$ref: "./schemas/feedback.yaml#/FeedbackItemCreate"
./schemas/feedback.yaml
# 1. Base Schema with Common/Client-Provided Fields
FeedbackItemBase:
type: object
properties:
content:
type: string
minLength: 1
description: The content of the feedback. Cannot be empty.
author:
type: string
description: Optional name or identifier of the person providing feedback.
example: "Jane Doe"
required:
# Fields required when SUBMITTING feedback
- content
# 2. Schema for CREATING a feedback item (Client -> Server POST Body)
FeedbackItemCreate:
description: Data required to create a new feedback item.
allOf: # Inherit all properties and requirements from FeedbackItemBase
- $ref: "#/FeedbackItemBase"
# 3. Schema for REPRESENTING an existing feedback item (Server -> Client Response)
FeedbackItem:
description: Represents an existing feedback item resource.
allOf: # Inherit properties and requirements from FeedbackItemBase
- $ref: "#/FeedbackItemBase"
# Define properties UNIQUE to the full FeedbackItem object
properties:
id:
type: string
description: Unique identifier for the feedback item (server-generated).
readOnly: true # Indicate server-generated
createdAt:
type: string
format: date-time
description: Timestamp when the feedback was submitted (server-generated).
example: "2025-03-29T13:01:15Z"
readOnly: true # Indicate server-generated
# List ONLY the ADDITIONAL required fields for the full object.
# 'content' is implicitly required via WorkshopBase. 'author' remains optional.
required:
- id
- timestamp
FeedbackCategory:
type: object
properties:
name:
type: string
minLength: 1
description: Name of the feedback category. Cannot be empty.
example: "Clarity"
readOnly: true
content:
type: string
minLength: 1
description: Content or description for the feedback category. Cannot be empty.
example: "Feedback related to how clear the topic was explained."
readOnly: true
imagePrompt:
type: string
description: The prompt used to generate the category image (if applicable).
example: "A lightbulb turning on over a confused student's head, cartoon style"
readOnly: true
imageUrl:
type: string
format: url
description: Optional URL of an image representing the category. Must be a valid URL format.
example: "https://example.com/images/clarity.png"
readOnly: true
required:
- name
- content
- imagePrompt # Mark required as per zod schema (not optional)
Summary:
type: object
properties:
categories:
type: array
items:
$ref: "#/FeedbackCategory"
minItems: 1 # Corresponds to .nonempty()
description: An array containing feedback categories. Must contain at least one category.
shared:
type: boolean
default: false # Corresponds to .default(false)
description: Indicates if the summary is shared. Defaults to false.
required:
- categories # `shared` is not required because it has a default
Used commandline:
$ datamodel-codegen --input ./schemas/openapi.yaml --input-file-type openapi --output ../../apps/backend/app/generated/models.py --output-model-type pydantic_v2.BaseModel --use-schema-description --use-field-description --field-constraints
Expected behavior
Generate Pydantic types successfully even if FeebackItemBase is not included in the openapi.yaml file since it's already referenced by FeedbackItem and FeedbackItemCreate that are already included it it.
Version:
- OS: Ubuntu
- Python version: 3.12.3
- datamodel-code-generator version: 0.28.5
Additional context Add any other context about the problem here.