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

Newline in generated reference URL #1019

Closed
magnusja opened this issue Sep 3, 2024 · 3 comments · Fixed by #1080
Closed

Newline in generated reference URL #1019

magnusja opened this issue Sep 3, 2024 · 3 comments · Fixed by #1080
Labels
bug Something isn't working investigate Futher investigation needed before other action

Comments

@magnusja
Copy link

magnusja commented Sep 3, 2024

Hey there,

thanks for this library first of all.

For some weird reason I have a newline in a reference URL.

#[utoipa::path(
get,
path = "/calculations/assembly-costs",
params(CalculationAssemblyCostParams),
responses(
(status = 200, description = "Get calculated cost of an assembly.",
    body = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)
),
)]

yields

"/calculations/assembly-costs": {
      "get": {
        "tags": [
          "routes::calculation_assembly_cost"
        ],
        "operationId": "get_single_assembly_cost_v1",
        "parameters": [
          {
            "name": "assembly_id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Get calculated cost of an assembly.",
            "content": {
              "application/json": {
                "schema": {
                  /// BAD
                  "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1 ::\nCalculationAssemblyCostResponse"
                }
              }
            }
          }
        }
      }
    },

Unfotunately this throws off the swagger editor. But I really dont understand why the newline is added there in the first place?

@juhaku
Copy link
Owner

juhaku commented Sep 3, 2024

Hmm, super interesting, There shouldn't be new line though. This is something that needs to be investigated. What version of utoipa you are using?

However the way the body reference names are going to be resolved in future will change in the new 5.0.0 release once it is out. The path will not end up defining the name anymore. But it will be solely controlled with as attribute if any prefixing is needed & wanted.

@juhaku juhaku added the investigate Futher investigation needed before other action label Sep 3, 2024
@magnusja
Copy link
Author

In my Cargo.toml is utoipa = "4", so I assume it uses the latest version.

#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
#[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
pub struct CalculationAssemblyCostResponse {
    #[schema(value_type = uuid::Uuid)]
    pub id: CalculationAssemblyCostId,
    #[schema(value_type = uuid::Uuid)]
    pub calculation_assembly_details: CalculationAssemblyDetailsId,
    pub costs: Vec<types::calculation::calculation_assembly_cost::v1::ScenarioCostResponse>,
    #[schema(value_type = String, format = "date-time")]
    pub created_at: NaiveDateTime,
    #[schema(value_type = String, format = "date-time")]
    pub updated_at: NaiveDateTime,
}

This is how the struct itself looks.

@juhaku
Copy link
Owner

juhaku commented Oct 3, 2024

This seems to be issue in the latest rc as well. I try to fix this. Also it seems that it generates the name for the type incorrectly by the as attribute.

@juhaku juhaku moved this to In Progress in utoipa kanban Oct 3, 2024
@juhaku juhaku added the bug Something isn't working label Oct 3, 2024
juhaku added a commit that referenced this issue Oct 3, 2024
Prior to this commit the `as` attribute produced invalid path with line
break within the name and some colons not being replaced. This commit
will fix this where now as definition like following will get serialized
correctly to JSON as shown below.
```rust
 #[derive(ToSchema)]
 #[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
 pub struct CalculationAssemblyCostResponse {
     #[schema(value_type = uuid::Uuid)]
     pub id: String,
 }
```
Will serialize to:
```json
 "schema": {
     "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1.CalculationAssemblyCostResponse"
 },
```

Fixes #1019
juhaku added a commit that referenced this issue Oct 3, 2024
Prior to this commit the `as` attribute produced invalid path with line
break within the name and some colons not being replaced. This commit
will fix this where now as definition like following will get serialized
correctly to JSON as shown below.
```rust
 #[derive(ToSchema)]
 #[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
 pub struct CalculationAssemblyCostResponse {
     #[schema(value_type = uuid::Uuid)]
     pub id: String,
 }
```
Will serialize to:
```json
 "schema": {
     "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1.CalculationAssemblyCostResponse"
 },
```

Fixes #1019
juhaku added a commit that referenced this issue Oct 3, 2024
Prior to this commit the `as` attribute produced invalid path with line
break within the name and some colons not being replaced. This commit
will fix this where now as definition like following will get serialized
correctly to JSON as shown below.
```rust
 #[derive(ToSchema)]
 #[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
 pub struct CalculationAssemblyCostResponse {
     #[schema(value_type = uuid::Uuid)]
     pub id: String,
 }
```
Will serialize to:
```json
 "schema": {
     "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1.CalculationAssemblyCostResponse"
 },
```

Fixes #1019
juhaku added a commit that referenced this issue Oct 3, 2024
Prior to this commit the `as` attribute produced invalid path with line
break within the name and some colons not being replaced. This commit
will fix this where now as definition like following will get serialized
correctly to JSON as shown below.
```rust
 #[derive(ToSchema)]
 #[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
 pub struct CalculationAssemblyCostResponse {
     #[schema(value_type = uuid::Uuid)]
     pub id: String,
 }
```
Will serialize to:
```json
 "schema": {
     "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1.CalculationAssemblyCostResponse"
 },
```

Fixes #1019
juhaku added a commit that referenced this issue Oct 3, 2024
Prior to this commit the `as` attribute produced invalid path with line
break within the name and some colons not being replaced. This commit
will fix this where now as definition like following will get serialized
correctly to JSON as shown below.
```rust
 #[derive(ToSchema)]
 #[schema(as = types::calculation::calculation_assembly_cost::v1::CalculationAssemblyCostResponse)]
 pub struct CalculationAssemblyCostResponse {
     #[schema(value_type = uuid::Uuid)]
     pub id: String,
 }
```
Will serialize to:
```json
 "schema": {
     "$ref": "#/components/schemas/types.calculation.calculation_assembly_cost.v1.CalculationAssemblyCostResponse"
 },
```

Fixes #1019
@github-project-automation github-project-automation bot moved this from In Progress to Done in utoipa kanban Oct 3, 2024
@juhaku juhaku moved this from Done to Released in utoipa kanban Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigate Futher investigation needed before other action
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

2 participants