Skip to content

Conversation

@walsha2
Copy link
Contributor

@walsha2 walsha2 commented Feb 12, 2025

Closes #79 Where String schema alias was not being properly written. For the following schema:

"schemas": {
  "StringAlias": {
    "type": "string"
  },
  "MyObject": {
    "type": "object",
    "properties": {
      "myField": {
        "$ref": "#/components/schemas/StringAlias"
      }
    }
  }
}

The generator now properly creates the expected types:

typedef StringAlias = String;
@freezed
class MyObject with _$MyObject {
  const MyObject._();

  /// Factory constructor for MyObject
  const factory MyObject({
    /// No Description
    @JsonKey(includeIfNull: false) StringAlias? myField,
  }) = _MyObject;

  /// Object construction from a JSON representation
  factory MyObject.fromJson(Map<String, dynamic> json) =>
      _$MyObjectFromJson(json);

  /// List of all property names of schema
  static const List<String> propertyNames = ['myField'];

  /// Perform validations on the schema property values
  String? validateSchema() {
    return null;
  }

  /// Map representation of object (not serialized)
  Map<String, dynamic> toMap() {
    return {
      'myField': myField,
    };
  }
}

To run this test and inspect the generated code:

make test TEST_ARGS="-n Misc"

@walsha2 walsha2 enabled auto-merge (squash) February 12, 2025 06:38
@walsha2 walsha2 merged commit 5f9c37a into main Feb 12, 2025
1 check passed
@walsha2 walsha2 deleted the issue-79 branch February 12, 2025 06:40
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.

Unable to dereference an schema 'alias'

2 participants