Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper deserialization of JSON type when top of level of data is array #404

Open
forksumit opened this issue Jun 23, 2024 · 2 comments
Open
Assignees
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@forksumit
Copy link

forksumit commented Jun 23, 2024

When inserting list<dict> (ex: [{"a": "b"}, {"x": "y"}]) into a JSON type via SQLAlchemy, insert works fine but when reading the same record, it gets deserialized to an empty JsonObject ({})

Environment details

  • Programming language: Python
  • OS: MacOS / Linux
  • Language runtime version: 3.11.4
  • Package version:
    • sqlalchemy-spanner==1.7.0
    • google-cloud-spanner==3.47.0
    • SQLAlchemy==2.0.30

Steps to reproduce

import sqlalchemy as sqla
from sqlalchemy.orm import Session, declarative_base

PROJECT_ID = "****"
INSTANCE_ID = "spanner-test"
DATABASE_ID = "sumit-poc"
DATABASE_URL = f"spanner+spanner:///projects/{PROJECT_ID}/instances/{INSTANCE_ID}/databases/{DATABASE_ID}"

engine = sqla.create_engine(DATABASE_URL)

metadata = sqla.MetaData()
ORMModelBase = declarative_base(metadata=metadata)


class SampleModel(ORMModelBase):
    __tablename__ = "SampleModel"

    id = sqla.Column("id", sqla.types.Integer, primary_key=True)
    data = sqla.Column("data", sqla.types.JSON, nullable=False)


if __name__ == "__main__":
    session = Session(engine)

    d = {"a": "b"}

    r = SampleModel(id=101, data=d)
    session.add(r)
    r = SampleModel(id=102, data=[d])
    session.add(r)
    session.commit()

    r1 = session.query(SampleModel).filter(SampleModel.id == 101).first()
    print("r1.data", r1.data)
    r2 = session.query(SampleModel).filter(SampleModel.id == 102).first()
    print("r2.data", r2.data)
    session.commit()

Expected Output:

r1.data {'a': 'b'}
r2.data [{'a': 'b'}]

Got Output:

r1.data {'a': 'b'}
r2.data {}
@forksumit forksumit added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Jun 23, 2024
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. label Jun 23, 2024
@forksumit
Copy link
Author

Spanner lib returns a JsonObject when datatype is JSON, spanner dialect again tries to deserialize the same via JsonObject() which returns an improper object

Related issue: googleapis/python-spanner#1154

@forksumit
Copy link
Author

Should be fixed by googleapis/python-spanner#1166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants