Skip to content

Commit

Permalink
feat: Support for OepnAPI3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Nov 21, 2022
1 parent 7ee492f commit 46686cf
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 40,512 deletions.
1,624 changes: 0 additions & 1,624 deletions packages/katana_model_openapi/example/lib/swagger.openapi.dart

This file was deleted.

30,976 changes: 0 additions & 30,976 deletions packages/katana_model_openapi/example/lib/swagger.openapi.freezed.dart

This file was deleted.

2,527 changes: 0 additions & 2,527 deletions packages/katana_model_openapi/example/lib/swagger.openapi.g.dart

This file was deleted.

4,434 changes: 0 additions & 4,434 deletions packages/katana_model_openapi/example/lib/swagger.openapi.yaml

This file was deleted.

9 changes: 5 additions & 4 deletions packages/katana_model_openapi/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ packages:
source: hosted
version: "6.5.4"
katana:
dependency: "direct overridden"
dependency: transitive
description:
path: "../../katana"
relative: true
source: path
name: katana
url: "https://pub.dartlang.org"
source: hosted
version: "0.15.10"
katana_model_openapi:
dependency: "direct main"
Expand Down Expand Up @@ -564,3 +564,4 @@ packages:
version: "3.1.1"
sdks:
dart: ">=2.18.1 <3.0.0"
flutter: ">=2.0.0"
913 changes: 0 additions & 913 deletions packages/katana_model_openapi/example/test/widget_test.dart

This file was deleted.

20 changes: 10 additions & 10 deletions packages/katana_model_openapi_builder/lib/common/freezed_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ part of katana_model_openapi_builder;

/// Creates classes created by Freezed.
///
/// Pass the map generated from OepnAPI to [definitions].
/// Pass the map generated from OepnAPI to [schemas].
///
/// Freezedで作成されるクラスを作成します。
///
/// [definitions]にOepnAPIから生成されたマップを渡します。
/// [schemas]にOepnAPIから生成されたマップを渡します。
List<Spec> freezedClass(
Map<String, APISchemaObject> definitions,
Map<String, APISchemaObject> schemas,
) {
return [
...definitions.entries.mapAndRemoveEmpty((define) {
final parameters = define.value.properties ?? {};
final required = define.value.required ?? [];
...schemas.entries.mapAndRemoveEmpty((schema) {
final parameters = schema.value.properties ?? {};
final required = schema.value.required ?? [];
return Class(
(c) => c
..name = define.key.toPascalCase()
..name = schema.key.toPascalCase()
..annotations.addAll([
const Reference("freezed"),
])
..mixins.addAll([Reference("_\$${define.key.toPascalCase()}")])
..mixins.addAll([Reference("_\$${schema.key.toPascalCase()}")])
..constructors.addAll([
Constructor(
(c) => c
Expand Down Expand Up @@ -48,7 +48,7 @@ List<Spec> freezedClass(
])
..factory = true
..constant = true
..redirect = Reference("_${define.key.toPascalCase()}"),
..redirect = Reference("_${schema.key.toPascalCase()}"),
),
Constructor(
(c) => c
Expand All @@ -62,7 +62,7 @@ List<Spec> freezedClass(
])
..factory = true
..lambda = true
..body = Code("_\$${define.key.toPascalCase()}FromJson(json)"),
..body = Code("_\$${schema.key.toPascalCase()}FromJson(json)"),
)
]),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OpenAPIGenerator {
/// Actual data from OepnAPI.
///
/// OepnAPIの実データ。
final v2.APIDocument api;
final APIDocument api;

/// Base name of the file.
///
Expand All @@ -38,7 +38,8 @@ class OpenAPIGenerator {
///
/// ライブラリーを生成します。
FutureOr<Library> generate() async {
final definitions = (api.definitions ?? {})
final schemas = (api.components?.schemas ?? {})
.map((key, value) => MapEntry(key.toPascalCase(), value))
..removeWhere((key, value) => value == null);
final builder = LibraryBuilder()
..directives.addAll([
Expand All @@ -47,7 +48,7 @@ class OpenAPIGenerator {
Directive.part(freezedPartFileName),
])
..body.addAll([
...freezedClass(definitions.cast()),
...freezedClass(schemas.cast()),
]);

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ library katana_model_openapi_builder;

import 'dart:async';
import 'dart:convert';
import 'dart:ffi';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:katana/katana.dart';
import 'package:open_api_forked/v2.dart' as v2;
import 'package:open_api_forked/v2.dart';
import 'package:open_api_forked/v3.dart' as v3;
import 'package:source_gen/source_gen.dart' as source_gen;
import 'package:open_api_forked/v3.dart';
import 'package:yaml/yaml.dart';

part 'src/builder.dart';
Expand Down
6 changes: 3 additions & 3 deletions packages/katana_model_openapi_builder/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OpenApiCodeBuilder extends Builder {
}
final inputIdBasename =
inputId.pathSegments.last.replaceAll(".openapi.yaml", "");
final api = _additionalDefinitions(
final api = _additionalComponentSchemas(
_loadApiFromYaml(source),
);

Expand Down Expand Up @@ -72,10 +72,10 @@ class OpenApiCodeBuilder extends Builder {
return libraryOutput;
}

v2.APIDocument _loadApiFromYaml(String yamlSource) {
APIDocument _loadApiFromYaml(String yamlSource) {
final tmp = loadYaml(yamlSource);
final decoded = jsonDecode(jsonEncode(tmp)) as DynamicMap?;
return v2.APIDocument.fromMap(
return APIDocument.fromMap(
_filter(
null,
Map<String, dynamic>.from(decoded?.cast<String, dynamic>() ?? {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension APISchemaObjectExtensions on APISchemaObject {
}) {
final nullable = !inList && !required.contains(key) ? "?" : "";
if (referenceURI != null) {
return "${referenceURI!.toString().split("/").last}$nullable";
return "${referenceURI!.toString().split("/").last.toPascalCase()}$nullable";
}
switch (type) {
case APIType.string:
Expand All @@ -21,8 +21,6 @@ extension APISchemaObjectExtensions on APISchemaObject {
return "bool$nullable";
case APIType.array:
return "List<${items?.toDartType(key, inList: true)}>";
case APIType.file:
return "file$nullable";
default:
return "Object$nullable";
}
Expand Down
17 changes: 9 additions & 8 deletions packages/katana_model_openapi_builder/lib/src/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,35 @@ DynamicMap _filter(String? parentKey, DynamicMap map) {
return res;
}

APIDocument _additionalDefinitions(APIDocument api) {
final definitions = api.definitions;
if (definitions == null) {
APIDocument _additionalComponentSchemas(APIDocument api) {
final schemas = api.components?.schemas;
if (schemas == null) {
return api;
}
final res = <String, APISchemaObject?>{};
for (final entry in definitions.entries) {
for (final entry in schemas.entries) {
for (final property in (entry.value?.properties ?? {}).entries) {
if (property.value?.referenceURI == null) {
final url = "${entry.key.toPascalCase()}${property.key.toPascalCase()}";
if (property.value?.type == APIType.object) {
res["${entry.key.toPascalCase()}${property.key.toPascalCase()}"] =
property.value;
property.value?.referenceURI = Uri.parse("#/definitions/$url");
property.value?.referenceURI = Uri.parse("#/components/schemas/$url");
} else if (property.value?.type == APIType.array &&
property.value?.items?.type == APIType.object) {
res["${entry.key.toPascalCase()}${property.key.toPascalCase()}"] =
property.value?.items;
property.value?.items?.referenceURI = Uri.parse("#/definitions/$url");
property.value?.items?.referenceURI =
Uri.parse("#/components/schemas/$url");
}
}
}
}
for (final tmp in res.entries) {
if (definitions.containsKey(tmp.key)) {
if (schemas.containsKey(tmp.key)) {
continue;
}
definitions[tmp.key] = tmp.value;
schemas[tmp.key] = tmp.value;
}
return api;
}

0 comments on commit 46686cf

Please sign in to comment.