Skip to content

Commit

Permalink
Merge branch 'master' into update/scalafmt-core-3.7.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Javakky-pxv authored Aug 29, 2023
2 parents d91d4e1 + b36c630 commit b3d7f1f
Show file tree
Hide file tree
Showing 26 changed files with 1,287 additions and 518 deletions.
541 changes: 532 additions & 9 deletions README.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
organization in ThisBuild := "com.iheart"
organization in ThisBuild := "io.github.play-swagger"

sonatypeCredentialHost := "s01.oss.sonatype.org"

ThisBuild / scalafixDependencies ++= Seq(
"com.github.liancheng" %% "organize-imports" % "0.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import scala.reflect.runtime.universe._

import com.fasterxml.jackson.databind.{BeanDescription, ObjectMapper}
import com.github.takezoe.scaladoc.Scaladoc
import com.iheart.playSwagger.Domain.{CustomMappings, Definition, GenSwaggerParameter, SwaggerParameter}
import com.iheart.playSwagger.SwaggerParameterMapper.mapParam
import com.iheart.playSwagger.Domain.Definition
import com.iheart.playSwagger.domain.parameter.{GenSwaggerParameter, SwaggerParameter}
import com.iheart.playSwagger.generator.SwaggerParameterMapper
import net.steppschuh.markdowngenerator.MarkdownElement
import net.steppschuh.markdowngenerator.link.Link
import net.steppschuh.markdowngenerator.table.Table
Expand All @@ -18,8 +19,7 @@ import net.steppschuh.markdowngenerator.text.heading.Heading
import play.routes.compiler.Parameter

final case class DefinitionGenerator(
modelQualifier: DomainModelQualifier = PrefixDomainModelQualifier(),
mappings: CustomMappings = Nil,
mapper: SwaggerParameterMapper,
swaggerPlayJava: Boolean = false,
_mapper: ObjectMapper = new ObjectMapper(),
namingStrategy: NamingStrategy = NamingStrategy.None,
Expand Down Expand Up @@ -109,7 +109,7 @@ final case class DefinitionGenerator(
val typeName = parametricType.resolve(rawTypeName)
// passing None for 'fixed' and 'default' here, since we're not dealing with route parameters
val param = Parameter(name, typeName, None, None)
mapParam(param, modelQualifier, mappings, paramDescriptions.get(field.name.decodedName.toString))
mapper.mapParam(param, paramDescriptions.get(field.name.decodedName.toString))
}
}

Expand All @@ -119,7 +119,7 @@ final case class DefinitionGenerator(
)
}

private def definitionForPOJO(tpe: Type): Seq[Domain.SwaggerParameter] = {
private def definitionForPOJO(tpe: Type): Seq[SwaggerParameter] = {
val m = runtimeMirror(cl)
val clazz = m.runtimeClass(tpe.typeSymbol.asClass)
val `type` = _mapper.constructType(clazz)
Expand All @@ -142,7 +142,7 @@ final case class DefinitionGenerator(
generalTypeName
}
val param = Parameter(name, typeName, None, None)
mapParam(param, modelQualifier, mappings)
mapper.mapParam(param, None)
}
}

Expand All @@ -166,9 +166,9 @@ final case class DefinitionGenerator(
case None =>
val thisDef = definition(defName)
val refNames: Seq[String] = for {
p thisDef.properties.collect(genSwaggerParameter)
className findRefTypes(p)
if modelQualifier.isModel(className)
p <- thisDef.properties.collect(genSwaggerParameter)
className <- findRefTypes(p)
if mapper.isReference(className)
} yield className

refNames.foldLeft(thisDef :: memo) { (foundDefs, refName) =>
Expand All @@ -185,27 +185,23 @@ final case class DefinitionGenerator(

object DefinitionGenerator {
def apply(
domainNameSpace: String,
customParameterTypeMappings: CustomMappings,
mapper: SwaggerParameterMapper,
swaggerPlayJava: Boolean,
namingStrategy: NamingStrategy
)(implicit cl: ClassLoader): DefinitionGenerator =
DefinitionGenerator(
PrefixDomainModelQualifier(domainNameSpace),
customParameterTypeMappings,
new DefinitionGenerator(
mapper,
swaggerPlayJava,
namingStrategy = namingStrategy
)

def apply(
domainNameSpace: String,
customParameterTypeMappings: CustomMappings,
mapper: SwaggerParameterMapper,
namingStrategy: NamingStrategy,
embedScaladoc: Boolean
)(implicit cl: ClassLoader): DefinitionGenerator =
DefinitionGenerator(
PrefixDomainModelQualifier(domainNameSpace),
customParameterTypeMappings,
new DefinitionGenerator(
mapper,
namingStrategy = namingStrategy,
embedScaladoc = embedScaladoc
)
Expand Down
61 changes: 1 addition & 60 deletions core/src/main/scala/com/iheart/playSwagger/Domain.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.iheart.playSwagger

import play.api.libs.json.{JsObject, JsPath, JsValue, Reads}
import com.iheart.playSwagger.domain.parameter.SwaggerParameter

object Domain {
type Path = String
Expand All @@ -11,63 +11,4 @@ object Domain {
properties: Seq[SwaggerParameter],
description: Option[String] = None
)

sealed trait SwaggerParameter {
def name: String
def required: Boolean
def nullable: Option[Boolean]
def default: Option[JsValue]
def description: Option[String]

def update(required: Boolean, nullable: Boolean, default: Option[JsValue]): SwaggerParameter
}

final case class GenSwaggerParameter(
name: String,
referenceType: Option[String] = None,
`type`: Option[String] = None,
format: Option[String] = None,
required: Boolean = true,
nullable: Option[Boolean] = None,
default: Option[JsValue] = None,
example: Option[JsValue] = None,
items: Option[SwaggerParameter] = None,
enum: Option[Seq[String]] = None,
description: Option[String] = None
) extends SwaggerParameter {
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]): GenSwaggerParameter =
copy(required = _required, nullable = Some(_nullable), default = _default)
}

final case class CustomSwaggerParameter(
name: String,
specAsParameter: List[JsObject],
specAsProperty: Option[JsObject],
required: Boolean = true,
nullable: Option[Boolean] = None,
default: Option[JsValue] = None,
description: Option[String] = None
) extends SwaggerParameter {
def update(_required: Boolean, _nullable: Boolean, _default: Option[JsValue]): CustomSwaggerParameter =
copy(required = _required, nullable = Some(_nullable), default = _default)
}

type CustomMappings = List[CustomTypeMapping]

case class CustomTypeMapping(
`type`: String,
specAsParameter: List[JsObject] = Nil,
specAsProperty: Option[JsObject] = None,
required: Boolean = true
)

object CustomTypeMapping {
import play.api.libs.functional.syntax._
implicit val csmFormat: Reads[CustomTypeMapping] = (
(JsPath \ 'type).read[String] and
(JsPath \ 'specAsParameter).read[List[JsObject]] and
(JsPath \ 'specAsProperty).readNullable[JsObject] and
((JsPath \ 'required).read[Boolean] orElse Reads.pure(true))
)(CustomTypeMapping.apply _)
}
}
Loading

0 comments on commit b3d7f1f

Please sign in to comment.