Skip to content

Commit

Permalink
Updates to OpenAPIGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Aug 1, 2024
1 parent 77b80fa commit 289f241
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "spice"
ThisBuild / organization := "com.outr"
ThisBuild / version := "0.5.12-SNAPSHOT"
ThisBuild / version := "0.5.12-SNAPSHOT1"

val scala213: String = "2.13.14"

Expand Down
4 changes: 4 additions & 0 deletions openapi/src/main/resources/generator/dart/model.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

%%IMPORTS%%
Expand All @@ -14,4 +15,7 @@ class %%CLASSNAME%% %%EXTENDS%%{
static %%CLASSNAME%% fromJson(Map<String, dynamic> json) => _$%%CLASSNAME%%FromJson(json);

%%TOJSON%%

@override
List<Object?> get props => [%%PROPS%%];
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:copy_with_extension/copy_with_extension.dart';

Expand All @@ -16,4 +17,7 @@ class %%CLASSNAME%% %%EXTENDS%%{
static %%CLASSNAME%% fromJson(Map<String, dynamic> json) => _$%%CLASSNAME%%FromJson(json);

%%TOJSON%%

@override
List<Object?> get props => [%%PROPS%%];
}
2 changes: 2 additions & 0 deletions openapi/src/main/resources/generator/dart/parent.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/// GENERATED CODE: Do not edit!
abstract class %%CLASSNAME%% {
%%FIELDS%%

%%CLASSNAME%%();

factory %%CLASSNAME%%.fromJson(Map<String, dynamic> json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ object OpenAPIDartGenerator extends OpenAPIGenerator {
case "json" => "Map<String, dynamic>"
case _ => throw new RuntimeException(s"Unsupported dart type: [$s]")
}
def param: String = {
val n = renameMap.getOrElse(s, s)
s"this.$n"
}
def param: String = s"this.$prop"
def prop: String = renameMap.getOrElse(s, s)
}

private implicit class OpenAPIContentExtras(content: OpenAPIContent) {
Expand Down Expand Up @@ -85,8 +83,38 @@ object OpenAPIDartGenerator extends OpenAPIGenerator {
val typeName = tn.replace(" ", "")
if (!parentFiles.contains(typeName)) {
val children = config.baseNames.find(_._1 == typeName).get._2.toList.sorted
var imports = children.toSet
val maps = children.map { child =>
val component = api.components.get.schemas(child).asInstanceOf[OpenAPISchema.Component]
component.properties.map {
case (key, schema) =>
val `type` = schema match {
case c: OpenAPISchema.Component if c.`type` == "array" => c.`type` // TODO: Support
case c: OpenAPISchema.Component if c.nullable.contains(true) => s"${c.`type`.dartType}?"
case c: OpenAPISchema.Component => c.`type`.dartType
case r: OpenAPISchema.Ref =>
val c = r.ref.substring(r.ref.lastIndexOf('/') + 1)
imports += c
if (r.nullable.contains(true)) {
s"$c?"
} else {
c
}
}
val k = key match {
case "_id" => "id"
case _ => key
}
k -> `type`
}
}
val commonKeys = maps.map(_.keySet).reduce(_ intersect _)
val baseParams = commonKeys.map(key => key -> maps.head(key)).toMap
val fields = baseParams.toList.map {
case (key, value) => s"$value get $key;";
}.mkString("\n ")
val fileName = s"${typeName.type2File}.dart"
val imports = children.map { c =>
val importString = imports.map { c =>
s"import '${c.type2File}.dart';"
}.mkString("\n")
val fromJson = children.map { c =>
Expand All @@ -99,7 +127,8 @@ object OpenAPIDartGenerator extends OpenAPIGenerator {
| }""".stripMargin)
val source = ParentTemplate
.replace("%%CLASSNAME%%", typeName)
.replace("%%IMPORTS%%", imports)
.replace("%%FIELDS%%", fields)
.replace("%%IMPORTS%%", importString)
.replace("%%FROMJSON%%", fromJson)
parentFiles += typeName -> SourceFile(
language = "Dart",
Expand Down Expand Up @@ -224,12 +253,13 @@ object OpenAPIDartGenerator extends OpenAPIGenerator {
} else {
""
}
val props = schema.properties.toList.map(_._1.prop).mkString(", ")
val parent = config.baseForTypeMap.get(tn)
val extending = parent match {
case Some(parentName) =>
imports = imports + parentName.type2File
s"extends $parentName "
case None => ""
s"extends $parentName with EquatableMixin "
case None => "extends Equatable "
}
val importsTemplate = imports.toList.sorted.map(s => s"import '$s.dart';").mkString("\n") match {
case "" => "// No imports necessary"
Expand All @@ -251,6 +281,7 @@ object OpenAPIDartGenerator extends OpenAPIGenerator {
.replace("%%EXTENDS%%", extending)
.replace("%%FIELDS%%", fields)
.replace("%%PARAMS%%", params)
.replace("%%PROPS%%", props)
.replace("%%TOJSON%%", toJson)
SourceFile(
language = "Dart",
Expand Down

0 comments on commit 289f241

Please sign in to comment.