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

SeaORM CLI not adding serde derives to Enums #461

Closed
BenJeau opened this issue Jan 16, 2022 · 2 comments · Fixed by #463
Closed

SeaORM CLI not adding serde derives to Enums #461

BenJeau opened this issue Jan 16, 2022 · 2 comments · Fixed by #463
Milestone

Comments

@BenJeau
Copy link
Contributor

BenJeau commented Jan 16, 2022

I'm currently playing with sea-orm coming from diesel-rs and I love it! But I ran into this error while using enums and serde, the enums do not contain any serde derives.

Reproduction

With a simple PostgreSQL schema

CREATE TYPE "user_kind_type" AS ENUM ('host', 'client');

CREATE TABLE users (
  "id" SERIAL PRIMARY KEY,
  "kind" "user_kind_type" NOT NULL
);

Running the following command (same behaviour with either serialize, deserialize, both):

sea-orm-cli generate entity -o src/entities --with-serde serialize

Creates the following file with Enums that does not contain the serde derives, where the errors propagates to the users model's kind column.

//! SeaORM Entity. Generated by sea-orm-codegen 0.5.0

use sea_orm::entity::prelude::*;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_kind_type")]
pub enum UserKindType {
    #[sea_orm(string_value = "client")]
    Client,
    #[sea_orm(string_value = "host")]
    Host,
}

Expected Result

I would expect enums, just like models, to contain the derive.

//! SeaORM Entity. Generated by sea-orm-codegen 0.5.0

use sea_orm::entity::prelude::*;
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Serialize)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_kind_type")]
pub enum UserKindType {
    #[sea_orm(string_value = "client")]
    Client,
    #[sea_orm(string_value = "host")]
    Host,
}
@BenJeau
Copy link
Contributor Author

BenJeau commented Jan 16, 2022

Just by looking at the source code, seems like enums are not treating imports the same way as entities.

Enums (https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L156) don't seem to care about serde whereas entities are using this function for generating imports (https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L231).

And as for the derives, entities take care of this here https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/writer.rs#L509, whereas enums are not https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/src/entity/active_enum.rs#L21.

Seems like it has been forgotten unless it was intentional? I can look into creating a PR adding serde derives to enums if it is wanted in this crate.

@billy1624
Copy link
Member

billy1624 commented Jan 17, 2022

Seems like it has been forgotten unless it was intentional?

I forgot to do so :(

I can look into creating a PR adding serde derives to enums if it is wanted in this crate.

Definitely wanted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants