Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify model and prop meta #1359

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 113 additions & 162 deletions modules/core/src/main/scala/dev/guardrail/SwaggerUtil.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,11 @@ object ProtocolGenerator {
for {
typeName <- formatTypeName(name).map(formattedName => NonEmptyList.of(hierarchy.name, formattedName))
propertyRequirement = getPropertyRequirement(prop, requiredFields.contains(name), defaultPropertyRequirement)
customType <- SwaggerUtil.customTypeName(prop)
resolvedType <- SwaggerUtil.propMeta[L, F](prop)
defValue <- defaultValue(typeName, prop, propertyRequirement, definitions)
fieldName <- formatFieldName(name)
customType <- SwaggerUtil.customTypeName(prop)
resolvedType <- SwaggerUtil
.propMeta[L, F](prop) // TODO: This should be resolved via an alternate mechanism that maintains references all the way through, instead of re-deriving and assuming that references are valid
defValue <- defaultValue(typeName, prop, propertyRequirement, definitions)
fieldName <- formatFieldName(name)
res <- transformProperty(hierarchy.name, dtoPackage, supportPackage, concreteTypes)(
name,
fieldName,
Expand Down
2 changes: 1 addition & 1 deletion modules/microsite/src/main/scala/DocsHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object DocsHelpers {
val o = q"object ${Term.Name(className)} { ..${definitions} }"
val Right(q"""class ${name }(...${args }) {
..${defns }
}""" ) = (g.client.head: @unchecked)
}""") = (g.client.head: @unchecked)
val basePath = defns.collectFirst {
case v @ q"val basePath: String = $_" => v
}
Expand Down
3 changes: 2 additions & 1 deletion modules/sample/src/main/resources/response-headers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ components:
schemas:
Foo:
type: object
required:
- name
properties:
name:
type: string
required: true
paths:
/foo:
get:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,12 @@ class Http4sServerGenerator private (version: Http4sVersion)(implicit Cl: Collec
for {
_ <- Target.log.debug("Found that not all parameters could be represented as unescaped terms")
res <- parameters.traverse[Target, LanguageParameter[ScalaLanguage]] { param =>
val newName = Term.Name(s"_${param.paramName.value}")
Target.log.debug(s"Escaping param ${param.argName.value}").flatMap { _ =>
if (newName.syntax == newName.value) Target.pure(param.withParamName(newName))
for {
_ <- Target.log.debug(s"Escaping param ${param.argName.value}")
newName = Term.Name(s"_${param.paramName.value}")
res <- if (newName.syntax == newName.value) Target.pure(param.withParamName(newName))
else Target.raiseUserError(s"Can't escape parameter with name ${param.argName.value}.")
}
} yield res
}
} yield res
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Issue144 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| description: description
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Ensure mapRoute is generated") {
val (_, _, Servers(Server(_, _, _, genResource :: _) :: Nil, _)) = runSwaggerSpec(swagger)(Context.empty, new Http4s(version))

Expand Down Expand Up @@ -63,7 +63,6 @@ class Issue144 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
// Cause structure is slightly different but source code is the same the value converted to string and then parsed
genResource.toString().parse[Stat].get.structure shouldEqual resource.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Issue165 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| description: description
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Ensure routes are generated") {
val (_, _, Servers(Server(_, _, genHandler, genResource :: _) :: Nil, Nil)) = runSwaggerSpec(swagger)(Context.empty, new Http4s(version))

Expand Down Expand Up @@ -88,7 +88,6 @@ class Issue165 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
// Cause structure is slightly different but source code is the same the value converted to string and then parsed
genResource.toString().parse[Stat].get.structure shouldEqual resource.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Issue166 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| type: string
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Handle generation of models") {
val opts = new ParseOptions()
opts.setResolve(true)
Expand Down Expand Up @@ -73,7 +73,6 @@ class Issue166 extends AnyFunSuite with Matchers with SwaggerSpecRunner {

cls.structure should equal(definition.structure)
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Issue223 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| description: uuid of kernel
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test uuid format generation") {
val (
ProtocolDefinitions(ClassDefinition(_, _, _, c1, _, _) :: Nil, _, _, _, _),
Expand All @@ -43,7 +43,6 @@ class Issue223 extends AnyFunSuite with Matchers with SwaggerSpecRunner {

c1.structure shouldBe q"case class Kernel(id: java.util.UUID)".structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Issue225 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| description: description
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Ensure mapRoute is generated") {
val (_, _, Servers(Server(_, _, genHandler, genResource :: _) :: Nil, Nil)) = runSwaggerSpec(swagger)(Context.empty, new Http4s(version))

Expand Down Expand Up @@ -61,7 +61,6 @@ class Issue225 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
// Cause structure is slightly different but source code is the same the value converted to string and then parsed
genResource.toString().parse[Stat].get.structure shouldEqual resource.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Issue255 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| x-scala-type: custom.Bytes
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test password format generation") {
val (
ProtocolDefinitions(ClassDefinition(_, _, _, c1, _, _) :: Nil, _, _, _, _),
Expand All @@ -59,7 +59,6 @@ class Issue255 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
"""
c1.structure shouldBe expected.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.scalatest.matchers.should.Matchers

class Issue260 extends AnyFunSpec with Matchers with SwaggerSpecRunner {

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
describe(version.toString()) {
describe("LocalDate path param is generated more than once") {

Expand Down Expand Up @@ -93,7 +93,6 @@ class Issue260 extends AnyFunSpec with Matchers with SwaggerSpecRunner {
}
}
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Issue370 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| default: x
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test nested enum definition") {
val (
ProtocolDefinitions(ClassDefinition(_, _, _, c1, s, _) :: _, _, _, _, _),
Expand Down Expand Up @@ -115,7 +115,6 @@ class Issue370 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
c1.structure shouldEqual q"case class Foo(value: Option[Foo.Value] = Option(Foo.Value.A), value2: Baz = Baz.X, nested: Option[Foo.Nested] = None)".structure
companion.structure shouldEqual cmp.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Issue416 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| type: string
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Ensure mapRoute is generated") {
val (_, _, Servers(Server(_, _, genHandler, genResource :: _) :: Nil, _)) = runSwaggerSpec(swagger)(Context.empty, new Http4s(version))

Expand Down Expand Up @@ -70,7 +70,6 @@ class Issue416 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
// Cause structure is slightly different but source code is the same the value converted to string and then parsed
genResource.toString().parse[Stat].get.structure shouldEqual resource.structure
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Issue420 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| - $ref: "#/definitions/Bar"
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test ordering") {
val (
ProtocolDefinitions(List(bar: ClassDefinition[ScalaLanguage], foo: ClassDefinition[ScalaLanguage]), _, _, _, _),
Expand All @@ -43,7 +43,6 @@ class Issue420 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
cmp(bar.cls, q"case class Bar(id: Option[String] = None)")
cmp(foo.cls, q"case class Foo(id: Option[String] = None, otherId: Option[String] = None)")
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Issue429 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
| enum: [0, 1]
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test correct escaping of numbers used as identifiers") {
val (
ProtocolDefinitions(ClassDefinition(_, _, _, _, staticDefns, _) :: Nil, _, _, _, _),
Expand Down Expand Up @@ -55,7 +55,6 @@ class Issue429 extends AnyFunSuite with Matchers with SwaggerSpecRunner {
statusCodeCompanion.structure shouldBe expected.structure
Term.Name("10").syntax shouldBe "`10`"
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FullyQualifiedNames extends AnyFunSuite with Matchers with SwaggerSpecRunn
| $ref: '#/definitions/User'
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Test that fully qualified names are used") {
val (
ProtocolDefinitions(List(clz @ ClassDefinition(_, _, fullType, _, _, _)), _, _, _, _),
Expand Down Expand Up @@ -82,7 +82,6 @@ class FullyQualifiedNames extends AnyFunSuite with Matchers with SwaggerSpecRunn
respObject shouldEqual q"""object GetUserResponse { case class Ok(value: _root_.com.test.User) extends GetUserResponse }"""

}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class DefaultParametersTest extends AnyFunSuite with Matchers with SwaggerSpecRu
| name: Order
|""".stripMargin

def testVersion(version: Http4sVersion): Unit = {
def testVersion(version: Http4sVersion): Unit =
test(s"$version - Ensure responses are generated") {
val (
_,
Expand Down Expand Up @@ -212,7 +212,6 @@ class DefaultParametersTest extends AnyFunSuite with Matchers with SwaggerSpecRu

statements.zip(expected).foreach({ case (a, b) => a.structure should equal(b.structure) })
}
}

testVersion(Http4sVersion.V0_22)
testVersion(Http4sVersion.V0_23)
Expand Down