-
Notifications
You must be signed in to change notification settings - Fork 85
Add inline fields support on union type entries #961
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,6 +52,7 @@ private[api_json] case class InternalApiJsonForm( | |||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def unions: Seq[InternalUnionForm] = declaredUnions ++ internalDatatypeBuilder.unionForms | ||||||
|
|
||||||
| private def parseModels(js: JsValue, prefix: Option[String]): Seq[InternalModelForm] = { | ||||||
|
|
@@ -274,6 +275,7 @@ case class InternalUnionTypeForm( | |||||
| attributes: Seq[InternalAttributeForm], | ||||||
| default: Option[Boolean], | ||||||
| discriminatorValue: Option[String], | ||||||
| fields: Option[Seq[InternalFieldForm]], | ||||||
| warnings: ValidatedNec[String, Unit] | ||||||
| ) | ||||||
|
|
||||||
|
|
@@ -448,20 +450,44 @@ object InternalUnionForm { | |||||
| val internalDatatype = internalDatatypeBuilder.parseTypeFromObject(json) | ||||||
| val datatypeName = internalDatatype.toOption.map(_.name) | ||||||
|
|
||||||
| val fields: Option[Seq[InternalFieldForm]] = | ||||||
| (json \ "fields").asOpt[JsArray].map { _ => | ||||||
| InternalFieldForm.parse(internalDatatypeBuilder, json) | ||||||
| } | ||||||
|
|
||||||
| fields.filter(_.nonEmpty).foreach { fieldForms => | ||||||
| datatypeName.foreach { name => | ||||||
| internalDatatypeBuilder.addDynamicModel( | ||||||
| InternalModelForm( | ||||||
| name = name, | ||||||
| plural = Text.pluralize(name), | ||||||
| description = JsonUtil.asOptString(json \ "description"), | ||||||
| deprecation = InternalDeprecationForm.fromJsValue(json), | ||||||
| fields = fieldForms, | ||||||
| attributes = Nil, | ||||||
| interfaces = Nil, | ||||||
| templates = Nil, | ||||||
| warnings = ().validNec | ||||||
| ) | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+458
to
+474
|
||||||
|
|
||||||
| InternalUnionTypeForm( | ||||||
| datatype = internalDatatype, | ||||||
| description = JsonUtil.asOptString(json \ "description"), | ||||||
| deprecation = InternalDeprecationForm.fromJsValue(json), | ||||||
| default = JsonUtil.asOptBoolean(json \ "default"), | ||||||
| discriminatorValue = JsonUtil.asOptString(json \ "discriminator_value"), | ||||||
| fields = fields, | ||||||
| attributes = InternalAttributeForm.fromJson((value \ "attributes").asOpt[JsArray]), | ||||||
|
||||||
| attributes = InternalAttributeForm.fromJson((value \ "attributes").asOpt[JsArray]), | |
| attributes = InternalAttributeForm.fromJson((json \ "attributes").asOpt[JsArray]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new
fieldssupport, the code will currently attempt to auto-generate models for anytypevalue (including primitives likestringor container types like[guest]/map[foo]) whenfieldsis present. This can create invalid or conflicting models (e.g., model namedstring) while the union member still resolves as a primitive/list. Add validation thatfieldscan only be used with a plain, non-primitive singleton type name (i.e., not a primitive, list, or map), and surface a clear error when violated.