diff --git a/pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json b/pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json index 83bcc7be8..e461b49d4 100644 --- a/pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json +++ b/pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json @@ -17,22 +17,14 @@ "type": "string", "anyOf": [ { - "const": "arm" - }, - { - "const": "arm64" - }, - { - "const": "ia32" - }, - { - "const": "riscv32" - }, - { - "const": "riscv64" - }, - { - "const": "x64" + "enum": [ + "arm", + "arm64", + "ia32", + "riscv32", + "riscv64", + "x64" + ] }, { "type": "string" @@ -288,19 +280,13 @@ "type": "string", "anyOf": [ { - "const": "dynamic_loading_bundle" - }, - { - "const": "dynamic_loading_executable" - }, - { - "const": "dynamic_loading_process" - }, - { - "const": "dynamic_loading_system" - }, - { - "const": "static" + "enum": [ + "dynamic_loading_bundle", + "dynamic_loading_executable", + "dynamic_loading_process", + "dynamic_loading_system", + "static" + ] }, { "type": "string" @@ -333,22 +319,14 @@ "type": "string", "anyOf": [ { - "const": "dynamic" - }, - { - "const": "prefer_dynamic" - }, - { - "const": "prefer_static" - }, - { - "const": "prefer-dynamic" - }, - { - "const": "prefer-static" - }, - { - "const": "static" + "enum": [ + "dynamic", + "prefer_dynamic", + "prefer-dynamic", + "prefer_static", + "prefer-static", + "static" + ] }, { "type": "string" @@ -370,19 +348,13 @@ "type": "string", "anyOf": [ { - "const": "android" - }, - { - "const": "ios" - }, - { - "const": "linux" - }, - { - "const": "macos" - }, - { - "const": "windows" + "enum": [ + "android", + "ios", + "linux", + "macos", + "windows" + ] }, { "type": "string" diff --git a/pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart b/pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart index 7a4290b2c..014d1e05f 100644 --- a/pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart +++ b/pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart @@ -86,9 +86,7 @@ class SchemaAnalyzer { typeName ??= _ucFirst(_snakeToCamelCase(name!)); if (_classes[typeName] != null) return; // Already analyzed. - final anyOf = schemas.anyOfs.single; - final values = - anyOf.map((e) => e.constValue).whereType().toList()..sort(); + final values = schemas.enumOrTaggedUnionValues; _classes[typeName] = EnumClassInfo( name: typeName, enumValues: [ @@ -243,9 +241,7 @@ class SchemaAnalyzer { final propertyKey = schemas.propertyKeys.single; final typeProperty = schemas.property(propertyKey); - final anyOf = typeProperty.anyOfs.single; - final subTypes = - anyOf.map((e) => e.constValue).whereType().toList(); + final subTypes = typeProperty.enumOrTaggedUnionValues; for (final subType in subTypes) { final ifThenSchemas = schemas.ifThenSchemas; final subTypeNameShort = _ucFirst(_snakeToCamelCase(subType)); @@ -591,6 +587,12 @@ extension type JsonSchemas._(List _schemas) { return result.singleOrNull; } + List get enum_ => [ + for (final schema in _schemas) + for (final value in schema.enumValues ?? []) + if (value is String) value, + ]; + List get refs { final result = []; for (final schema in _schemas) { @@ -695,6 +697,14 @@ extension on JsonSchemas { } throw StateError('No super class schema found for $parentClassName.'); } + + List get enumOrTaggedUnionValues => [ + for (final schema in _schemas) + for (final s in schema.anyOf) ...[ + if (s.constValue is String) s.constValue as String, + ...s.enumValues?.whereType() ?? [], + ], + ]..sort(); } extension on String {