Skip to content

One of support for lagom #6964

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions generated/src/main/scala/api/AppleReqDisc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These outputs under generated/ should be deleted

* fruity
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.0.1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package api
import play.api.libs.json._

final case class AppleReqDisc extends Fruit (
seeds: Int,
)

object AppleReqDisc {
implicit val format: Format[AppleReqDisc] = Json.format
}


24 changes: 24 additions & 0 deletions generated/src/main/scala/api/BananaReqDisc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* fruity
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.0.1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package api
import play.api.libs.json._

final case class BananaReqDisc extends Fruit (
length: Int,
)

object BananaReqDisc {
implicit val format: Format[BananaReqDisc] = Json.format
}


39 changes: 39 additions & 0 deletions generated/src/main/scala/api/Fruit.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* fruity
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.0.1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package api
import play.api.libs.json._

trait Fruit (
fruitType: String,
)

object Fruit {
private val discriminator = "fruitType"

implicit val format: OFormat[Fruit] = new OFormat[Fruit]{
override def reads(json: JsValue): JsResult[Fruit] =
json.validate[JsObject].flatMap(
_.value(discriminator).as[String] match {
case "AppleReqDisc" => json.validate[AppleReqDisc]
case "BananaReqDisc" => json.validate[BananaReqDisc]
}
)

override def writes(o: Fruit): JsObject = o match {
case a: AppleReqDisc => AppleReqDisc.format.writes(a) + (discriminator -> JsString("AppleReqDisc"))
case a: BananaReqDisc => BananaReqDisc.format.writes(a) + (discriminator -> JsString("BananaReqDisc"))
}
}
}


25 changes: 25 additions & 0 deletions generated/src/main/scala/api/FruitBasket.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* fruity
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.0.1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package api
import play.api.libs.json._

final case class FruitBasket (
quantity: Option[Int],
fruit: Option[Fruit]
)

object FruitBasket {
implicit val format: Format[FruitBasket] = Json.format
}


Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static org.openapitools.codegen.utils.StringUtils.camelize;
import static org.openapitools.codegen.utils.StringUtils.underscore;
Expand Down Expand Up @@ -163,6 +164,42 @@ public String toOperationId(String operationId) {
return camelize(operationId, true);
}

private CodegenModel codegenModel(Object o) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper function may be confusing. It's extracting the top-level model from our object model but reads as if it intends to extract a model from any input object.

Renaming to extractTopLevelModel might make it clearer.

return ((Map<String, List<Map<String, CodegenModel>>>)o).get("models").get(0).get("model");
}

@Override
public Map<String, Object> postProcessAllModels(final Map<String, Object> objs) {

for(Object value : objs.values()) {
if(value instanceof Map) {
CodegenModel m = codegenModel(value);
// for oneOf schemas
if(!m.oneOf.isEmpty() && m.getDiscriminator() != null) {
// add it as a parent to every single model and removed unnecessary discriminator prop
m.oneOf.forEach(
subclassName -> {
CodegenModel cm = codegenModel(objs.get(subclassName));
cm.setParent(m.classname);
if(!cm.getVars().isEmpty()) {
cm.setVars(
cm.getVars().stream().filter(v -> !v.getName().equals(m.getDiscriminator().getPropertyName())).collect(Collectors.toList())
);
}
}
);
// retain only discriminator prop
if(!m.getVars().isEmpty()) {
m.setVars(
m.getVars().stream().filter(v -> v.getName().equals(m.getDiscriminator().getPropertyName())).collect(Collectors.toList())
);
}
}
}
}
return objs;
}

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
objs = super.postProcessModelsEnum(objs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,66 @@ import {{import}}

{{#models}}
{{#model}}
case class {{classname}} (
{{#vars}}
{{#isEnum}}
{{{name}}}: Option[{{classname}}{{datatypeWithEnum}}.{{classname}}{{datatypeWithEnum}}]{{#hasMore}},{{/hasMore}}
{{classname}}{{datatypeWithEnum}} extends Enumeration {
val {{#allowableValues}} {{#values}}{{.}}{{^-last}}, {{/-last}}{{/values}} = Value{{/allowableValues}}
type {{classname}}{{datatypeWithEnum}} = Value
implicit val format: Format[Value] = Format(Reads.enumNameReads(this), Writes.enumNameWrites[{{classname}}{{datatypeWithEnum}}.type])
}
{{/isEnum}}
{{^isEnum}}
{{#description}}
/* {{{description}}} */
{{/description}}
{{{name}}}: {{^required}}Option[{{/required}}{{dataType}}{{^required}}]{{/required}}{{#hasMore}},{{/hasMore}}
{{/isEnum}}
{{/vars}}
)
{{#discriminator}}
trait {{classname}} (
{{/discriminator}}
{{^discriminator}}
final case class {{classname}} {{#parent}}extends{{/parent}} {{parent}} (
{{/discriminator}}
{{#vars}}
{{#isEnum}}
{{{name}}}: Option[{{classname}}{{datatypeWithEnum}}.{{classname}}{{datatypeWithEnum}}]{{#hasMore}},{{/hasMore}}
{{/isEnum}}
{{^isEnum}}
{{#description}}
/* {{{description}}} */
{{/description}}
{{{name}}}: {{^required}}Option[{{/required}}{{dataType}}{{^required}}]{{/required}}{{#hasMore}},{{/hasMore}}
{{/isEnum}}
{{/vars}}
)

object {{classname}} {
{{#discriminator}}
private val discriminator = "{{discriminator.propertyBaseName}}"

implicit val format: OFormat[{{classname}}] = new OFormat[Fruit]{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fruit should not exist here…

override def reads(json: JsValue): JsResult[{{classname}}] =
json.validate[JsObject].flatMap(
_.value(discriminator).as[String] match {
{{#oneOf}}
case "{{.}}" => json.validate[{{.}}]
{{/oneOf}}
}
)

object {{classname}} {
implicit val format: Format[{{classname}}] = Json.format
}
override def writes(o: {{classname}}): JsObject = o match {
{{#oneOf}}
case a: {{.}} => {{.}}.format.writes(a) + (discriminator -> JsString("{{.}}"))
{{/oneOf}}
}
}
{{/discriminator}}
{{^discriminator}}
implicit val format: Format[{{classname}}] = Json.format
{{/discriminator}}
}

{{#vars}}
{{#isEnum}}
object {{classname}}{{datatypeWithEnum}} extends Enumeration {
val {{#allowableValues}} {{#values}}{{.}}{{^-last}}, {{/-last}}{{/values}} = Value{{/allowableValues}}
type {{classname}}{{datatypeWithEnum}} = Value
implicit val format: Format[Value] = Format(Reads.enumNameReads(this), Writes.enumNameWrites[{{classname}}{{datatypeWithEnum}}.type])
}
{{/isEnum}}
{{/vars}}
{{#vars}}
{{#isEnum}}
object {{classname}}{{datatypeWithEnum}} extends Enumeration {
}
{{/isEnum}}
{{/vars}}

{{/isEnum}}
{{/model}}
{{/models}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.1
info:
title: fruity
version: 0.0.1
paths:
/:
get:
responses:
'200':
description: desc
content:
application/json:
schema:
$ref: '#/components/schemas/FruitBasket'
components:
schemas:
FruitBasket:
properties:
quantity:
type: int
fruit:
$ref: '#/components/schemas/Fruit'
Fruit:
oneOf:
- $ref: '#/components/schemas/AppleReqDisc'
- $ref: '#/components/schemas/BananaReqDisc'
discriminator:
propertyName: fruitType
AppleReqDisc:
type: object
required:
- seeds
- fruitType
properties:
seeds:
type: integer
fruitType:
type: string
BananaReqDisc:
type: object
required:
- length
- fruitType
properties:
length:
type: integer
fruitType:
type: string