Skip to content

feature/default-values-support#1

Open
dordzhiev wants to merge 3 commits into
mtbo-org:masterfrom
dordzhiev:feature/default-values-support
Open

feature/default-values-support#1
dordzhiev wants to merge 3 commits into
mtbo-org:masterfrom
dordzhiev:feature/default-values-support

Conversation

@dordzhiev
Copy link
Copy Markdown

@dordzhiev dordzhiev commented Sep 29, 2025

Add Default Values Support for Dart Renderer

Summary

This PR implements comprehensive default value support for the Dart renderer in quicktype, allowing JSON schemas with default properties to generate Dart classes with proper default value initialization.

What's New

Core Features

  • Default Value Parsing: JSON Schema default properties are now parsed and stored in the type system
  • Dart Code Generation: Default values are properly formatted and included in generated Dart constructors
  • Type-Safe Defaults: Support for all Dart primitive types (string, number, boolean) and complex types (arrays, objects, enums)
  • Null Safety Integration: Default values work seamlessly with Dart's null safety features

Technical Implementation

Type System Extensions:

  • Extended ClassProperty to store default values
  • Updated TypeBuilder to handle default value parameters
  • Enhanced type equality and hashing to include default values

JSON Schema Integration:

  • Modified JSONSchemaInput to extract default properties from schema definitions
  • Proper handling of default values in object properties and additional properties

Dart Renderer Enhancements:

  • Added _formatDefaultValue() method for type-safe default value formatting
  • Enhanced constructor generation to include default values
  • Support for both regular classes and Freezed classes with @Default() annotations
  • Proper handling of nullable types with default values

Files Changed

Core Type System

  • packages/quicktype-core/src/Type/Type.ts - Extended ClassProperty with default value support
  • packages/quicktype-core/src/Type/TypeBuilder.ts - Updated property creation methods
  • packages/quicktype-core/src/Type/TypeGraphUtils.ts - Enhanced type utilities

Input Processing

  • packages/quicktype-core/src/input/JSONSchemaInput.ts - Added default value extraction from JSON Schema
  • packages/quicktype-core/src/input/Inference.ts - Updated inference logic

Dart Renderer

  • packages/quicktype-core/src/language/Dart/DartRenderer.ts - Major enhancements (203 lines added)
    • Comprehensive default value formatting
    • Constructor generation with defaults
    • Freezed class support with @Default() annotations
    • Null safety integration

Graph Processing

  • packages/quicktype-core/src/GraphRewriting.ts - Updated graph rewriting logic
  • packages/quicktype-core/src/UnifyClasses.ts - Enhanced class unification
  • packages/quicktype-core/src/rewrites/ReplaceObjectType.ts - Updated object type replacement
  • packages/quicktype-core/src/rewrites/ResolveIntersections.ts - Enhanced intersection resolution

Testing

  • test/inputs/schema/default-values.schema - Test schema with various default value types
  • test/inputs/schema/default-values.1.json - Test data for validation

Example Usage

Input JSON Schema:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "default": "John Doe"
    },
    "age": {
      "type": "integer", 
      "default": 25
    },
    "isActive": {
      "type": "boolean",
      "default": true
    },
    "tags": {
      "type": "array",
      "items": {"type": "string"},
      "default": ["default", "tag"]
    }
  }
}

Generated Dart Code:

class Example {
  final String name;
  final int age;
  final bool isActive;
  final List<String> tags;

  Example({
    this.name = "John Doe",
    this.age = 25,
    this.isActive = true,
    this.tags = const ["default", "tag"],
  });
}

Freezed Support:

@freezed
abstract class Example with _$Example {
  const factory Example({
    @Default("John Doe") String name,
    @Default(25) int age,
    @Default(true) bool isActive,
    @Default(["default", "tag"]) List<String> tags,
  }) = _Example;
}

Key Features

Supported Default Value Types

  • Primitives: string, number, boolean
  • Collections: array (generates empty list [])
  • Objects: Complex objects with nested default values
  • Enums: String values mapped to enum cases
  • Null: Proper null handling

Advanced Features

  • Nested Objects: Recursive default value construction for complex object hierarchies
  • Union Types: Default values work with optional fields and union types
  • Freezed Support: Uses @Default() annotations for Freezed classes
  • Null Safety: Proper integration with Dart's null safety system

Testing

The implementation includes comprehensive test cases covering:

  • Basic primitive default values
  • Array and object defaults
  • Optional fields with defaults
  • Complex nested structures
  • Edge cases and error handling

Impact

  • Enhanced Developer Experience: Developers can now use JSON schemas with default values to generate more complete Dart classes
  • Reduced Boilerplate: Generated classes include sensible defaults, reducing manual initialization code
  • Type Safety: All default values are type-checked and properly formatted for Dart
  • Framework Integration: Works seamlessly with popular Dart frameworks like Freezed

Breaking Changes

None - This is a purely additive feature that maintains backward compatibility.

Documentation

  • Updated README.md with default values examples
  • Added FAQ section explaining default values support
  • Clarified that default values currently work only for Dart language

Ready for Review

Dordzhiev Danzan added 2 commits September 30, 2025 10:14
This commit implements foundational default value functionality for the Dart
renderer, supporting primitive types and enums but not complex objects.
This commit extends the existing default value functionality to support
complex object types with nested structures, completing the implementation
of default values for all Dart data types.
@dordzhiev dordzhiev force-pushed the feature/default-values-support branch from 65b26bb to 891201b Compare September 30, 2025 07:19
- Add default values documentation to README and FAQ
- Clarify Dart-only support for default values
@dordzhiev dordzhiev changed the title Enhance TypeReconstituter and ClassProperty to support default values… feature/default-values-support Sep 30, 2025
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 this pull request may close these issues.

1 participant