-
Notifications
You must be signed in to change notification settings - Fork 10
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
fix for discriminated union transform #176
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 |
---|---|---|
|
@@ -40,11 +40,20 @@ class DiscriminatedUnionMemberComponents() extends OpenApiMapper { | |
.getModel() | ||
.getUnionShapesWithTrait(classOf[DiscriminatedUnionTrait]) | ||
val componentBuilder = openapi.getComponents().toBuilder() | ||
val componentSchemas: Map[String, Schema] = openapi | ||
val componentSchemas: Map[ShapeId, Schema] = openapi | ||
.getComponents() | ||
.getSchemas() | ||
.asScala | ||
.toMap | ||
.flatMap { case (_, schema) => | ||
schema | ||
.getExtension(DiscriminatedUnionShapeId.SHAPE_ID_KEY) | ||
.asScala | ||
.flatMap { node => | ||
node.toNode.asStringNode.asScala | ||
.map(s => ShapeId.from(s.getValue) -> schema) | ||
} | ||
} | ||
unions.asScala.foreach { union => | ||
val unionMixinName = union.getId().getName() + "Mixin" | ||
val unionMixinId = | ||
|
@@ -82,19 +91,17 @@ class DiscriminatedUnionMemberComponents() extends OpenApiMapper { | |
componentBuilder.putSchema(syntheticMemberName, syntheticUnionMember) | ||
} | ||
|
||
val existingSchemaBuilder = componentSchemas | ||
.get(union.toShapeId.getName) | ||
.map(_.toBuilder()) | ||
.getOrElse(Schema.builder()) | ||
componentBuilder.putSchema( | ||
union.toShapeId.getName, | ||
updateDiscriminatedUnion( | ||
union, | ||
existingSchemaBuilder, | ||
discriminatorField | ||
componentSchemas.get(union.toShapeId).foreach { sch => | ||
componentBuilder.putSchema( | ||
union.toShapeId.getName, | ||
updateDiscriminatedUnion( | ||
union, | ||
sch.toBuilder(), | ||
discriminatorField | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
} | ||
|
||
} | ||
openapi.toBuilder.components(componentBuilder.build()).build() | ||
|
@@ -130,6 +137,7 @@ class DiscriminatedUnionMemberComponents() extends OpenApiMapper { | |
.asJava | ||
) | ||
schemaBuilder | ||
.removeExtension(DiscriminatedUnionShapeId.SHAPE_ID_KEY) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extension so it is not included in the final OpenAPI file |
||
.oneOf(schemas) | ||
.putExtension( | ||
"discriminator", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright 2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package alloy.openapi | ||
|
||
import _root_.software.amazon.smithy.jsonschema.JsonSchemaConfig | ||
import _root_.software.amazon.smithy.jsonschema.JsonSchemaMapper | ||
import _root_.software.amazon.smithy.jsonschema.Schema.Builder | ||
import _root_.software.amazon.smithy.model.shapes.Shape | ||
import alloy.DiscriminatedUnionTrait | ||
|
||
import software.amazon.smithy.model.node.Node | ||
|
||
class DiscriminatedUnionShapeId() extends JsonSchemaMapper { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A JsonSchemaMapper has to be used to get access to the full ShapeId, OpenAPI mappers do not have this information available (as far as I can tell). |
||
|
||
import DiscriminatedUnionShapeId._ | ||
|
||
override def updateSchema( | ||
shape: Shape, | ||
schemaBuilder: Builder, | ||
config: JsonSchemaConfig | ||
): Builder = if (shape.hasTrait(classOf[DiscriminatedUnionTrait])) { | ||
schemaBuilder.putExtension( | ||
SHAPE_ID_KEY, | ||
Node.from(shape.toShapeId.toString) | ||
) | ||
} else schemaBuilder | ||
} | ||
|
||
object DiscriminatedUnionShapeId { | ||
private[openapi] val SHAPE_ID_KEY: String = "SHAPE_ID" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "BarService", | ||
"version": "" | ||
}, | ||
"paths": { | ||
"/bar": { | ||
"get": { | ||
"operationId": "BarOp", | ||
"responses": { | ||
"200": { | ||
"description": "BarOp200response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/BarOpResponseContent" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"BarOpResponseContent": { | ||
"type": "object", | ||
"properties": { | ||
"out": { | ||
"$ref": "#/components/schemas/CatOrDog" | ||
} | ||
} | ||
}, | ||
"CatOrDog": { | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"title": "one", | ||
"properties": { | ||
"one": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"one" | ||
] | ||
}, | ||
{ | ||
"type": "object", | ||
"title": "two", | ||
"properties": { | ||
"two": { | ||
"type": "integer", | ||
"format": "int32" | ||
} | ||
}, | ||
"required": [ | ||
"two" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$version: "2" | ||
|
||
namespace bar | ||
|
||
use alloy#simpleRestJson | ||
|
||
@simpleRestJson | ||
service BarService { | ||
operations: [BarOp] | ||
} | ||
|
||
@http(method: "GET", uri: "/bar") | ||
operation BarOp { | ||
output := { | ||
out: CatOrDog | ||
} | ||
} | ||
|
||
union CatOrDog { | ||
one: String | ||
two: Integer | ||
} |
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.
Change to using full ShapeId instead of just shape name