-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from disneystreaming/discriminated-unions-fix
fix for discriminated union transform
- Loading branch information
Showing
6 changed files
with
180 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
modules/openapi/src/alloy/openapi/DiscriminatedUnionShapeId.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters