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

issue #222 #234

Merged
merged 36 commits into from May 3, 2019
Merged

issue #222 #234

Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
062ffb5
Added test case for issue 222
Apr 9, 2019
08f9932
It finally compiles! Except for java :(
Apr 12, 2019
2ebdf07
Merge branch 'master' into issue222
tomasherman Apr 12, 2019
28b1b6b
Added test case for issue 222
Apr 25, 2019
cc1cc1b
Merge branch 'issue222' of github.com:avast/guardrail into issue222
Apr 25, 2019
184a5b9
Test passes
Apr 25, 2019
e014a04
Merge branch 'master' into issue222
tomasherman Apr 25, 2019
61d15f2
Reimplemented
Apr 25, 2019
5cd28fb
Merge branch 'issue222' of github.com:avast/guardrail into issue222
Apr 25, 2019
ee39921
Attempt to fix jacksonˆ
Apr 25, 2019
7a9b6da
Removed unused values
Apr 25, 2019
e525c60
Scan all parents for discriminators
Apr 25, 2019
db374e4
Cleanup
Apr 26, 2019
89cc0c4
Update modules/codegen/src/main/scala/com/twilio/guardrail/generators…
blast-hardcheese Apr 26, 2019
d2e7b96
Code review improvements #2
Apr 26, 2019
f244636
Merge branch 'master' into issue222
tomasherman Apr 26, 2019
cf0ddf9
Merge branch 'master' into issue222
tomasherman Apr 27, 2019
8c636a8
Removed comment
Apr 27, 2019
c3a8d6f
Merge branch 'issue222' of github.com:avast/guardrail into issue222
Apr 27, 2019
8411ede
Merge branch 'master' into issue222
tomasherman Apr 30, 2019
0efb82e
Merge branch 'master' of https://github.com/twilio/guardrail into iss…
May 1, 2019
e97efbd
Added another test case and implementation
May 1, 2019
0d54cab
Improved 2nd test case scenario
May 1, 2019
2a7e2fc
Merge branch 'issue222' of github.com:avast/guardrail into issue222
May 1, 2019
8aec398
Added another test case
May 1, 2019
83a5cfc
Added better error reporting when resolving references
May 1, 2019
bb38c38
Scalafmt
May 1, 2019
cb96779
Attempt to reduce filename sizes
May 1, 2019
f81912b
Update modules/codegen/src/main/scala/com/twilio/guardrail/generators…
blast-hardcheese May 2, 2019
5a75f7a
Merge branch 'master' of https://github.com/twilio/guardrail into iss…
May 2, 2019
20ad1f6
Merge branch 'issue222' of github.com:avast/guardrail into issue222
May 2, 2019
6c37f8a
Merge branch 'master' into issue222
tomasherman May 2, 2019
c0b3ddd
Merge branch 'master' into issue222
tomasherman May 2, 2019
fcbddde
Reverted changes to jackson generation
May 2, 2019
b5bfc35
Merge branch 'master' into issue222
tomasherman May 3, 2019
aa2ba14
Merge branch 'master' into issue222
blast-hardcheese May 3, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Added another test case

  • Loading branch information...
Tomas Herman
Tomas Herman committed May 1, 2019
commit 8aec398c14ae51dfd188d9c93570f16c3b287e92
@@ -377,7 +377,7 @@ object CirceProtocolGenerator {
definitions
.collectFirst({
case (clsName, e) if Option(head.get$ref).exists(_.endsWith(s"/$clsName")) =>
(clsName, e, tail.toList) :: allParents(e)
(clsName, e, tail) :: allParents(e)
})
.getOrElse(List.empty)
case _ => List.empty
@@ -7,72 +7,91 @@ import org.scalatest.{Assertion, FunSuite, Matchers}
import support.SwaggerSpecRunner

class Issue222 extends FunSuite with Matchers with SwaggerSpecRunner {

import scala.meta._

val swagger: String = s"""
|swagger: '2.0'
|info:
| title: someapp
| description: someapp
| version: '1'
|basePath: "/v1"
|schemes:
| - http
|produces:
| - application/json
|paths: {}
|definitions:
| Request:
| description: Request fields with id
| allOf:
| - "$$ref": "#/definitions/RequestFields"
| - type: object
| properties:
| id:
| type: string
| RequestFields:
| description: Request fields
| type: object
| properties:
| state:
| type: integer
| Request2:
| description: Request fields with id
| allOf:
| - "$$ref": "#/definitions/RequestFields2"
| - type: object
| properties:
| id:
| type: string
| - type: object
| properties:
| id2:
| type: string
| RequestFields2:
| description: Request fields
| type: object
| properties:
| state2:
| type: integer
|
|""".stripMargin
val swagger: String =
s"""
|swagger: '2.0'
|info:
| title: someapp
| description: someapp
| version: '1'
|basePath: "/v1"
|schemes:
| - http
|produces:
| - application/json
|paths: {}
|definitions:
| Request:
| description: Request fields with id
| allOf:
| - "$$ref": "#/definitions/RequestFields"
| - type: object
| properties:
| id:
| type: string
| RequestFields:
| description: Request fields
| type: object
| properties:
| state:
| type: integer
| required: [state]
| Request2:
| description: Request fields with id
| allOf:
| - "$$ref": "#/definitions/RequestFields2"
| - type: object
| properties:
| id:
| type: string
| - type: object
| properties:
| id2:
| type: string
| required: [id2]
| RequestFields2:
| description: Request fields
| type: object
| properties:
| state2:
| type: integer
| Request3:
| description: Request fields with id
| allOf:
| - "$$ref": "#/definitions/RequestFields"
| - type: object
| properties:
| id:
| type: string
| - type: object
| properties:
| id2:
| type: string
| required: [id2]
| - "$$ref": "#/definitions/RequestFields2"
|""".stripMargin

test("Ensure case-to-case inheritance is not generated") {
val (ProtocolDefinitions(List(request: ClassDefinition[ScalaLanguage], requestFields: ClassDefinition[ScalaLanguage], _, _), _, _, _), _, _) = runSwaggerSpec(swagger)(Context.empty, Http4s)
val (ProtocolDefinitions(List(request: ClassDefinition[ScalaLanguage], requestFields: ClassDefinition[ScalaLanguage], _, _, _), _, _, _), _, _) = runSwaggerSpec(swagger)(Context.empty, Http4s)

val List(reqEncoder, reqDecoder) = request.staticDefns.definitions

val expectedRequestTpe = t"""Request"""

val expectedRequestCls = q"""case class Request(state: Option[BigInt] = None, id: Option[String] = None)"""
val expectedRequestCls = q"""case class Request(state: BigInt, id: Option[String] = None)"""

val expectedRequestEncoder = q"""
val expectedRequestEncoder =
q"""
implicit val encodeRequest = {
val readOnlyKeys = Set[String]()
Encoder.forProduct2("state", "id")((o: Request) => (o.state, o.id)).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
}
"""
val expectedRequestDecoder = q"""
val expectedRequestDecoder =
q"""
implicit val decodeRequest = Decoder.forProduct2("state", "id")(Request.apply _)
"""

@@ -83,17 +102,19 @@ class Issue222 extends FunSuite with Matchers with SwaggerSpecRunner {
compare(reqDecoder, expectedRequestDecoder)

val expectedFieldsTpe = t"""RequestFields"""
val expectedFieldsCls = q"""case class RequestFields(state: Option[BigInt] = None)"""
val expectedFieldsCls = q"""case class RequestFields(state: BigInt)"""

val List(fieldsEncoder, fieldsDecoder) = requestFields.staticDefns.definitions

val expectedFieldsEncoder = q"""
val expectedFieldsEncoder =
q"""
implicit val encodeRequestFields = {
val readOnlyKeys = Set[String]()
Encoder.forProduct1("state")((o: RequestFields) => o.state).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
This conversation was marked as resolved by tomasherman

This comment has been minimized.

Copy link
@blast-hardcheese

blast-hardcheese Apr 30, 2019

Collaborator

Isn't "id" missing from the encoder?

This comment has been minimized.

Copy link
@tomasherman

tomasherman Apr 30, 2019

Author Contributor

what do you mean? there is no id in request fields, it's only in request itself ... unless im missing something?

This comment has been minimized.

Copy link
@blast-hardcheese

blast-hardcheese Apr 30, 2019

Collaborator

Ah, my apologies -- I encountered the error I mentioned below, and I was hasty in looking for an example of the undesired behaviour in what you already had here.

}
"""
val expectedFieldsDecoder = q"""
val expectedFieldsDecoder =
q"""
implicit val decodeRequestFields = Decoder.forProduct1("state")(RequestFields.apply _)
"""

@@ -103,22 +124,24 @@ class Issue222 extends FunSuite with Matchers with SwaggerSpecRunner {
compare(fieldsDecoder, expectedFieldsDecoder)
}

test("Ensure case-to-case inheritance is not generated, more complicated scenario") {
val (ProtocolDefinitions(List(_, _,request: ClassDefinition[ScalaLanguage], requestFields: ClassDefinition[ScalaLanguage]), _, _, _), _, _) = runSwaggerSpec(swagger)(Context.empty, Http4s)
test("Ensure case-to-case inheritance is not generated, extends two objects") {
val (ProtocolDefinitions(List(_, _, request: ClassDefinition[ScalaLanguage], requestFields: ClassDefinition[ScalaLanguage], _), _, _, _), _, _) = runSwaggerSpec(swagger)(Context.empty, Http4s)

val List(reqEncoder, reqDecoder) = request.staticDefns.definitions

val expectedRequestTpe = t"""Request2"""

val expectedRequestCls = q"""case class Request2(state2: Option[BigInt] = None, id: Option[String] = None, id2: Option[String] = None)"""
val expectedRequestCls = q"""case class Request2(state2: Option[BigInt] = None, id: Option[String] = None, id2: String)"""

val expectedRequestEncoder = q"""
val expectedRequestEncoder =
q"""
implicit val encodeRequest2 = {
val readOnlyKeys = Set[String]()
Encoder.forProduct3("state2", "id", "id2")((o: Request2) => (o.state2, o.id, o.id2)).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
}
"""
val expectedRequestDecoder = q"""
val expectedRequestDecoder =
q"""
implicit val decodeRequest2 = Decoder.forProduct3("state2", "id", "id2")(Request2.apply _)
"""

@@ -133,13 +156,15 @@ class Issue222 extends FunSuite with Matchers with SwaggerSpecRunner {

val List(fieldsEncoder, fieldsDecoder) = requestFields.staticDefns.definitions

val expectedFieldsEncoder = q"""
val expectedFieldsEncoder =
q"""
implicit val encodeRequestFields2 = {
val readOnlyKeys = Set[String]()
Encoder.forProduct1("state2")((o: RequestFields2) => o.state2).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
}
"""
val expectedFieldsDecoder = q"""
val expectedFieldsDecoder =
q"""
implicit val decodeRequestFields2 = Decoder.forProduct1("state2")(RequestFields2.apply _)
"""

@@ -149,7 +174,34 @@ class Issue222 extends FunSuite with Matchers with SwaggerSpecRunner {
compare(fieldsDecoder, expectedFieldsDecoder)
}

test("Ensure case-to-case inheritance is not generated, extends two objects and two classes") {
val (ProtocolDefinitions(List(_, _, _, _, request: ClassDefinition[ScalaLanguage]), _, _, _), _, _) = runSwaggerSpec(swagger)(Context.empty, Http4s)

val List(reqEncoder, reqDecoder) = request.staticDefns.definitions

val expectedRequestTpe = t"""Request3"""

val expectedRequestCls = q"""case class Request3(state: BigInt, state2: Option[BigInt] = None, id: Option[String] = None, id2: String)"""

val expectedRequestEncoder =
q"""
implicit val encodeRequest3 = {
val readOnlyKeys = Set[String]()
Encoder.forProduct4("state", "state2", "id", "id2")((o: Request3) => (o.state, o.state2, o.id, o.id2)).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
}
"""
val expectedRequestDecoder =
q"""
implicit val decodeRequest3 = Decoder.forProduct4("state", "state2", "id", "id2")(Request3.apply _)
"""


compare(request.tpe, expectedRequestTpe)
compare(request.cls, expectedRequestCls)
compare(reqEncoder, expectedRequestEncoder)
compare(reqDecoder, expectedRequestDecoder)

}


private def compare(t1: Tree, t2: Tree): Assertion = {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.