Skip to content

Commit

Permalink
Handle union types for array and map items schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
walsha2 committed Oct 11, 2023
1 parent 6e12907 commit 2b993e8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.14

* Handle union types for array and map items schemas

## 0.5.13

* Fix static analysis warning in generated files
Expand Down
29 changes: 23 additions & 6 deletions lib/src/generators/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class SchemaGenerator extends BaseGenerator {
orElse: () => p,
);
var (c, nullable) = propHeader(p.defaultValue, p.description);
var itemType = p.items.toDartType();
var itemType = p.items.toDartType(unions: _unions);
c += "List<$itemType> ${nullable ? '?' : ''} $name,\n\n";
file.writeAsStringSync(c, mode: FileMode.append);
},
Expand Down Expand Up @@ -657,21 +657,38 @@ class SchemaGenerator extends BaseGenerator {
}
}

// Check for unions in component schemas
for (final s in (spec.components?.schemas?.keys ?? <String>[])) {
spec.components?.schemas?[s]?.mapOrNull(
void recursiveSchemaSearch(Schema? schema) {
if (schema == null) {
return;
}
schema.mapOrNull(
object: (o) {
final props = o.properties;
final propNames = props?.keys.toList() ?? <String>[];
checkAnyOf(o.anyOf);
for (final pName in propNames) {
o.properties![pName]?.mapOrNull(
object: (p) => checkAnyOf(p.anyOf),
object: (p) {
checkAnyOf(p.anyOf);
recursiveSchemaSearch(p);
},
array: (a) => recursiveSchemaSearch(
a.items.mapOrNull(object: (o) => o),
),
map: (m) => recursiveSchemaSearch(
m.valueSchema?.mapOrNull(object: (o) => o),
),
);
}
},
);
}

// Check for unions in component schemas
for (final s in (spec.components?.schemas?.keys ?? <String>[])) {
recursiveSchemaSearch(spec.components?.schemas?[s]);
}

// Check for unions in component responses
for (final key in (spec.components?.responses?.keys ?? <String>[])) {
final r = spec.components?.responses?[key];
Expand All @@ -692,7 +709,7 @@ class SchemaGenerator extends BaseGenerator {
}
}

// Check for unions in path rquests/responses
// Check for unions in path requests/responses
for (final p in (spec.paths?.values ?? <PathItem>[])) {
// Responses
p.get?.responses?.forEach((_, r) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/open_api/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library openapi_models;
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:collection/collection.dart';
import 'package:recase/recase.dart';
import 'package:yaml/yaml.dart' as yaml;
import 'package:path/path.dart' as p;
Expand Down
5 changes: 2 additions & 3 deletions lib/src/open_api/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ class Schema with _$Schema {
object: (s) {
if (s.anyOf != null && unions != null) {
final subSchemas = s.anyOf!.map((e) => e.toDartType()).toList();
final leq = ListEquality();
for (final e in unions.entries) {
if (subSchemas.any((s) => !e.value.contains(s))) {
continue;
} else {
if (leq.equals(subSchemas, e.value)) {
final type = e.key.pascalCase;
if (s.nullable == true) {
return '$type?';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: openapi_spec
description: OpenAPI Specification generator using native Dart code, as well as an all-in-one parser of existing specifications.
version: 0.5.13
version: 0.5.14
maintainer: Taza Technology LLC
repository: https://github.com/tazatechnology/openapi_spec
issue_tracker: https://github.com/tazatechnology/openapi_spec/issues
Expand Down

0 comments on commit 2b993e8

Please sign in to comment.