Skip to content

Relative $ref does not resolve externally when using --url option #3080

@Raekkeri

Description

@Raekkeri

Describe the bug
In version 0.41.0 and later, a relative $ref is no longer resolved externally, but rather the file is being attempted to load from local filesystem.

To Reproduce

When generating with command datamodel-codegen --url http://localhost:8888/schemas/v1/main.schema.json --output Main --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel we get the following error:

Traceback (most recent call last):
...
FileNotFoundError: [Errno 2] No such file or directory: '/schemas/v1/sub.schema.json'

Example schema:

main.schema.json:

{
  "$id": "/schemas/v1/main.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Main",
  "type": "object",
  "properties": {
    "sub": {
      "$ref": "sub.schema.json"
    }
  }
}

sub.schema.json:

{
  "$id": "/schemas/v1/sub.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Sub",
  "type": "string",
  "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
}

Used commandline:

$ datamodel-codegen --url http://localhost:8888/schemas/v1/main.schema.json --output Main --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel

Expected behavior
We expected the tool to generate a directory named "Main" with the proper Pydantic classes.
Version:

  • OS: macOs
  • Python version: 3.10.15
  • datamodel-code-generator version: 0.41.0 or newer

Additional context

Would it be possible to add a command line option to specify the "base URL" to use for $ref resolving?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions