Skip to content

Support more json schema $ref patterns #1479

@Kilo59

Description

@Kilo59

Feature Request

I would like to be able to generate more types of references than can be created via a simple ref_prefix. For example, referencing a schema that resides in another file.

{ "$ref": "/schemas/Foo.json#/}

https://json-schema.org/understanding-json-schema/structuring.html#reuse
https://docs.opis.io/json-schema/1.x/pointers.html

Example

import json
from pydantic import BaseModel
from pydantic.schema import schema

class Foo(BaseModel):
    a: int

class Model(BaseModel):
    a: Foo

# Default location for OpenAPI
model_schema = Model.schema(ref_template='/schemas/{name}.json#/')
print(json.dumps(model_schema , indent=2))
{
  "Model": {
    "title": "Model",
    "type": "object",
    "properties": {
      "a": {
        "$ref": "/schemas/Foo.json#/"
      }
    },
    "required": ["a"]
  }
}

I believe python's string.Template could be a good way to implement this (which I have attempted).
There are still some details that need to be worked out.

Pull request

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions