Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- Fixed an incorrect double nested object for flattened fields.

## 0.19.3

#### 🚀 Updates
Expand Down
2 changes: 2 additions & 0 deletions crates/schematic/examples/show_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused)]

use schematic::*;

#[derive(Config)]
Expand Down
18 changes: 15 additions & 3 deletions crates/schematic/src/schema/renderers/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl SchemaRenderer<JsonSchema> for JsonSchemaRenderer {
) -> RenderResult<JsonSchema> {
let mut properties = BTreeMap::new();
let mut required = BTreeSet::from_iter(structure.required.clone().unwrap_or_default());
let mut additional_properties = JsonSchema::Bool(false);
let mut additional_properties = Some(Box::new(JsonSchema::Bool(false)));
let exclude_aliases = self.options.exclude_aliases;

for (name, field) in &structure.fields {
Expand All @@ -447,7 +447,19 @@ impl SchemaRenderer<JsonSchema> for JsonSchemaRenderer {
}

if field.flatten {
additional_properties = self.render_schema_without_reference(&field.schema)?;
if let Some(schema) = field.schema.get_nonnull_schema() {
let flattened = self.render_schema_without_reference(schema)?;

if matches!(schema.ty, SchemaType::Object(_))
&& let JsonSchema::Object(inner) = flattened.clone()
&& let Some(object) = inner.object
{
additional_properties = object.additional_properties;
} else {
additional_properties = Some(Box::new(flattened));
}
}

continue;
}

Expand All @@ -471,7 +483,7 @@ impl SchemaRenderer<JsonSchema> for JsonSchemaRenderer {
metadata: Some(Box::new(self.create_metadata_from_schema(schema))),
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
object: Some(Box::new(ObjectValidation {
additional_properties: Some(Box::new(additional_properties)),
additional_properties,
required,
properties,
..Default::default()
Expand Down
17 changes: 8 additions & 9 deletions crates/schematic/src/schema/renderers/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ impl TypeScriptRenderer {
fn export_object_type(&mut self, name: &str, schema: &Schema, value: String) -> RenderResult {
let mut tags = vec![];

let output = if !value.contains(" & ")
&& matches!(self.options.object_format, ObjectFormat::Interface)
let output = if matches!(self.options.object_format, ObjectFormat::Interface)
&& value.starts_with('{')
&& value.ends_with('}')
{
format!("export interface {name} {value}")
} else {
Expand Down Expand Up @@ -189,18 +190,16 @@ impl TypeScriptRenderer {
// Extract flattened fields first as we'll need to use intersections
// to support them correctly in TypeScript
for (field_name, field) in &structure.fields {
if field.flatten {
if field.flatten
&& let Some(schema) = field.schema.get_nonnull_schema()
{
let name = format!(
"{name}{}",
field_name.from_case(Case::Snake).to_case(Case::Pascal)
);
let value = self.render_schema(&field.schema)?;
let value = self.render_schema(schema)?;

outputs.push(self.export_object_type(
&name,
&field.schema,
format!("{{ [key: string]: {value} }}"),
)?);
outputs.push(self.export_object_type(&name, &field.schema, value)?);
extends.push(name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,14 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"definitions": {
"AnotherConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,14 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"definitions": {
"AnotherConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,13 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"anyOf": [
{
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
},
{
"type": "null"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"definitions": {
Expand Down Expand Up @@ -628,20 +615,14 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
}
},
"PartialAnotherConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,14 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"definitions": {
"AnotherConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,14 @@ expression: "fs::read_to_string(file).unwrap()"
}
},
"additionalProperties": {
"type": "object",
"additionalProperties": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"propertyNames": {
"type": "string"
}
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"definitions": {
"AnotherConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type AnotherConfig = {
opt: string | null,
};

export type GenConfigFlattened = { [key: string]: Record<string, unknown> };
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export type GenConfigBase = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down Expand Up @@ -83,7 +83,7 @@ export interface PartialAnotherConfig {
opt?: string | null;
}

export interface PartialGenConfigFlattened { [key: string]: Record<string, unknown> | null }
export type PartialGenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface PartialGenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnotherConfig {
opt?: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnotherConfig {
opt?: string | null | undefined;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface AnotherConfig {
opt: string | null;
}

export interface GenConfigFlattened { [key: string]: Record<string, unknown> }
export type GenConfigFlattened = Record<string, unknown>;

/** @deprecated */
export interface GenConfigBase {
Expand Down
Loading