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

fix: better models of oneOf - support for simple types #97

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/cli/src/commands/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl GetSchemaCommand for Opts {
impl Opts {
pub fn run(
&self,
schema: &mut Schema,
schema: &Schema,
discovery: &Discovery,
storage: &SchemaStorage,
) -> Result<(), Error> {
Expand Down Expand Up @@ -241,20 +241,20 @@ impl Opts {
}

pub fn execute(opts: Opts, client: &Client) -> Result<(), Error> {
let mut schema = opts.get_schema(client)?;
let schema = opts.get_schema(client)?;
let storage = &SchemaStorage::new(&schema, client);
let discovery = Discovery::default();

match &opts.command {
Command::JsonSchema(o) => {
o.verbose.start()?;

opts.run(&mut schema, &discovery, storage)
opts.run(&schema, &discovery, storage)
}
Command::Openapi(o) => {
o.verbose.start()?;

opts.run(&mut schema, &discovery, storage)
opts.run(&schema, &discovery, storage)
}
}
}
8 changes: 4 additions & 4 deletions crates/cli/src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl GetSchemaCommand for Opts {
}

impl Opts {
pub fn run(&self, schema: &mut Schema) -> Result<(), Error> {
pub fn run(&self, schema: &Schema) -> Result<(), Error> {
match &self.command {
Command::Openapi(_) => validate::validate_openapi(schema).map_err(Error::Schematools),
Command::JsonSchema(_) => {
Expand Down Expand Up @@ -108,16 +108,16 @@ impl Opts {
}

pub fn execute(opts: Opts, client: &Client) -> Result<(), Error> {
let mut schema = opts.get_schema(client)?;
let schema = opts.get_schema(client)?;

match &opts.command {
Command::Openapi(o) => {
o.verbose.start()?;
opts.run(&mut schema)
opts.run(&schema)
}
Command::JsonSchema(o) => {
o.verbose.start()?;
opts.run(&mut schema)
opts.run(&schema)
}
}
}
21 changes: 16 additions & 5 deletions crates/schematools/src/codegen/jsonschema/oneof/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ pub trait Extractor {
#[derive(Serialize)]
pub struct DiscriminatorMeta {
pub property: String,
pub value: String,
pub value: DiscriminatorValue,
pub properties: Option<usize>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DiscriminatorValue {
Model(String),
Simple(FlatModel),
}

pub struct Simple {
properties: Vec<SimpleProperty>,
}
Expand Down Expand Up @@ -115,8 +122,8 @@ impl Simple {
value: f
.model
.as_ref()
.and_then(|m| m.name.clone())
.unwrap_or(f.name.clone().unwrap()),
.and_then(|m| m.name.clone().map(DiscriminatorValue::Model))
.unwrap_or(DiscriminatorValue::Simple(f.clone())),
properties: Some(object.properties.len()),
}
})
Expand All @@ -131,7 +138,11 @@ impl Simple {

DiscriminatorMeta {
property: f.name.clone().unwrap(),
value: f.model.as_ref().and_then(|m| m.name.clone()).unwrap(),
value: f
.model
.as_ref()
.and_then(|m| m.name.clone().map(DiscriminatorValue::Model))
.unwrap_or(DiscriminatorValue::Simple(f.clone())),
properties: Some(object.properties.len() - 1),
}
})
Expand Down Expand Up @@ -213,7 +224,7 @@ impl Extractor for Discriminator {
DISCRIMINATOR_META.to_owned(),
serde_json::to_value(DiscriminatorMeta {
property: self.property.clone(),
value: value.clone(),
value: DiscriminatorValue::Model(value.clone()),
properties,
})
.unwrap(),
Expand Down
32 changes: 28 additions & 4 deletions crates/schematools/src/codegen/jsonschema/oneof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,19 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "some",
"value": "some",
"value": {
"simple": {
"name": "some",
"type": "string",
"model": null,
"required": true,
"nullable": false,
"validation": null,
"x": {},
"description": null,
"default": null
}
},
"properties": 1
})
)]
Expand All @@ -202,7 +214,19 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "testing",
"value": "testing",
"value": {
"simple": {
"name": "testing",
"type": "number",
"model": null,
"required": true,
"nullable": false,
"validation": null,
"x": {},
"description": null,
"default": null
}
},
"properties": 1
})
)]
Expand Down Expand Up @@ -263,7 +287,7 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "type",
"value": "value1",
"value": {"model": "value1"},
"properties": 1
})
)]
Expand All @@ -289,7 +313,7 @@ mod tests {
"_discriminator".to_string(),
json!({
"property": "type",
"value": "value2",
"value": {"model":"value2"},
"properties": 1
})
)]
Expand Down
2 changes: 1 addition & 1 deletion crates/schematools/src/codegen/jsonschema/required.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::scope::SchemaScope;
use serde_json::Map;
use serde_json::Value;

pub fn extract_required(data: &Map<String, Value>, scope: &mut SchemaScope) -> Vec<String> {
pub fn extract_required(data: &Map<String, Value>, scope: &SchemaScope) -> Vec<String> {
match data.get("required").unwrap_or(&serde_json::json!([])) {
Value::Array(a) => a.iter().map(|v| v.as_str().unwrap().to_string()).collect(),
_ => {
Expand Down
2 changes: 1 addition & 1 deletion crates/schematools/src/codegen/jsonschema/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl Default for Attributes {
}

impl AnyType {
pub fn model(schema: &Map<String, Value>, scope: &mut SchemaScope) -> Model {
pub fn model(schema: &Map<String, Value>, scope: &SchemaScope) -> Model {
log::debug!("{}: {:?} may be invalid json schema", scope, schema);

Model::new(ModelType::AnyType(Self {}))
Expand Down
4 changes: 2 additions & 2 deletions crates/schematools/src/codegen/openapi/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn extract_endpoints(
path: &str,
scope: &mut SchemaScope,
mcontainer: &mut ModelContainer,
scontainer: &mut security::SecuritySchemes,
scontainer: &security::SecuritySchemes,
resolver: &SchemaResolver,
options: &JsonSchemaExtractOptions,
) -> Result<Vec<Endpoint>, Error> {
Expand Down Expand Up @@ -93,7 +93,7 @@ fn new_endpoint(
method: &str,
scope: &mut SchemaScope,
mcontainer: &mut ModelContainer,
scontainer: &mut security::SecuritySchemes,
scontainer: &security::SecuritySchemes,
resolver: &SchemaResolver,
options: &JsonSchemaExtractOptions,
) -> Result<Endpoint, Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/schematools/src/codegen/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn extract(
tools::each_node(root, &mut scope, "path:security", |node, _parts, scope| {
scope.glue("security");

let schemes = security::extract_defaults(node, scope, &mut scontainer)?;
let schemes = security::extract_defaults(node, scope, &scontainer)?;
for scheme in schemes {
scontainer.add_default(scheme);
}
Expand Down Expand Up @@ -247,7 +247,7 @@ pub fn extract(
path,
scope,
&mut mcontainer,
&mut scontainer,
&scontainer,
resolver,
options,
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/schematools/src/codegen/openapi/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn new_scheme(
pub fn extract_defaults(
node: &Value,
scope: &mut SchemaScope,
scontainer: &mut SecuritySchemes,
scontainer: &SecuritySchemes,
) -> Result<Vec<SecurityScheme>, Error> {
match node {
Value::Array(scheme_names) => {
Expand All @@ -113,7 +113,7 @@ pub fn extract_defaults(

pub fn extract_default(
node: &Value,
scontainer: &mut SecuritySchemes,
scontainer: &SecuritySchemes,
) -> Result<Option<SecurityScheme>, Error> {
match node {
Value::Object(data) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/schematools/src/process/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn process_node(
}
}

fn process_discriminator(root: &mut Value, ctx: &mut DereferencerContext) {
fn process_discriminator(root: &mut Value, ctx: &DereferencerContext) {
log::debug!("{}: processing discriminator", ctx.scope);

if let Value::Object(ref mut map) = root {
Expand Down
Loading