oxidecomputer / oxidecomputer/typify

default values for floats/doubles are not rendered

Open
#442 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

Example

Schema

{
  "definitions": {
    "Data": {
      "type": "object",
      "properties": {
        "frequency": {
          "type": "number",
          "default": 0.1
        },
        "amount": {
          "type": "integer",
          "default": 42
        },
        "reason": {
          "type": "string",
          "default": "I'm a teapot"
        }
      }
    }
  }
}

Invocation

$ cargo typify --no-builder example.json

Output

#![allow(clippy::redundant_closure_call)]
#![allow(clippy::needless_lifetimes)]
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Data {
    #[serde(default = "defaults::default_u64::<i64, 42>")]
    pub amount: i64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frequency: Option<f64>,
    #[serde(default = "defaults::data_reason")]
    pub reason: String,
}
impl From<&Data> for Data {
    fn from(value: &Data) -> Self {
        value.clone()
    }
}
pub mod defaults {
    pub(super) fn default_u64<T, const V: u64>() -> T
    where
        T: std::convert::TryFrom<u64>,
        <T as std::convert::TryFrom<u64>>::Error: std::fmt::Debug,
    {
        T::try_from(V).unwrap()
    }
    pub(super) fn data_reason() -> String {
        "I'm a teapot".to_string()
    }
}

Expected

frequency is expected to not be optional and use a default function like reason.

Meta

$ cargo typify --version
cargo-typify 0.0.14

Real version is: c9d6453fc3cf69726d539925b838b267f886cb53 (main)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown JSON Schema and cargo typify --no-builder example.json, then inspect how numeric defaults are rendered in the generated Rust. Done means frequency is non-optional and uses a default function like reason, while the existing integer and string defaults remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.