Skip to main content

Automate JSON:API relationships with Pydantic. No manual mapping, just clean code.

Project description

japyd

"JSON:API, Pydantically simple."

Automate JSON:API relationships with Pydantic. No manual mapping, just clean code.

Tests Codecov PyPI License: MIT Python Maintenance Open Source

Description

To automate and standardize the definition of relationships in our JSON:API implementation, we leveraged Pydantic’s data model. This approach allowed us to dynamically infer relationships between resources without manually declaring them for each object type.

The principle is as follows:

  • A Pydantic model represents a resource (e.g., JSON:API Resource Object).
  • If an attribute of this model is itself a Pydantic object (or a list of Pydantic objects), it is automatically
  • interpreted as a relationship in the JSON:API response.
  • If the attribute is a primitive type (string, integer, boolean, or event dict etc.), it is treated as a standard attribute.

Usage

Define your data models in Pydantic, let japyd automatically handle serialization—including relationships and included resources—and expose a standard-compliant JSON:API with Flask in just a few lines of code.

import typing as t

import pytest
from flask import Flask
from flask_pydantic import validate

from japyd import JsonApiBaseModel
from japyd import JsonApiQueryModel
from japyd import TopLevel


class Product(JsonApiBaseModel):
    jsonapi_type: t.ClassVar[str] = "product"

    id: str
    price: float


class Order(JsonApiBaseModel):
    jsonapi_type: t.ClassVar[str] = "order"

    id: str
    customer_id: str
    items: list[Product]  # This attribute will be 'relationship' in JSON:API
    status: str  # This attribute will be classical 'attribute'


app = Flask(__name__)


@app.route("/orders/<order_id>")
@validate(exclude_none=True)
def get_order(order_id, query: JsonApiQueryModel):
    order = Order(id=order_id, customer_id="123", items=[Product(id="1", price=100.0)], status="open")
    return query.one_or_none(order)


@pytest.fixture()
def client():
    return app.test_client()


def test_request(client):
    response = client.get("/orders/3?include=items")
    top = TopLevel.model_validate(response.json)
    assert top.data.id == "3"
    assert top.data.attributes['status'] == 'open'
    assert len(top.data.relationships['items'].data) == 1
    assert top.included[0].type == "product"

References

japyd (JsonApi PYDantic) is a coherent and powerful composition of :

  1. Pydantic and its Flask extension Flask-Pydantic
  2. Filtering syntax defined in the dotnet implementation JsonApiDotNetCore.
  3. Simple relationship extraction and other structure manipulations.

🚀 Looking for Contributors!

We’re actively seeking developers, testers, and open-source enthusiasts to help us build and improve japyd. Whether you’re passionate about data validation, API design, or just want to contribute to an innovative open-source project, your help is welcome! Check out our contribution guidelines and open issues to get started. Let’s shape the future of Python APIs together! 💻✨

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

japyd-0.1.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

japyd-0.1.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file japyd-0.1.2.tar.gz.

File metadata

  • Download URL: japyd-0.1.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.6 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for japyd-0.1.2.tar.gz
Algorithm Hash digest
SHA256 891681c9d3d89c575285b87d8b0e9725d0685ce7d158dc575842f5dfc9ee2d9d
MD5 255d3b5146f39faf74aee90ed3434cc2
BLAKE2b-256 7da5bbf4fda5b5bebef93012ee7dcd158afb58ee6ad0cebe45486055c8a23f6f

See more details on using hashes here.

File details

Details for the file japyd-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: japyd-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.6 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for japyd-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f95fadea53006a7f30c800f075f4379963670ec44e2566161053e7ff6595c772
MD5 47f1f298f628d7f5a82059e8fef39b07
BLAKE2b-256 ed7c9ef62ec258e5df077bd0dc2df4bb5eb267f5ea90ed489dd8cdba949d8c2b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page