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

update openapi/jsonschema conversion to translate jsonUnknown to additionalProperties #183

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ final class AlloyOpenApiExtension() extends Smithy2OpenApiExtension {
new DataExamplesMapper(),
new ExternalDocumentationMapperJsonSchema(),
new NullableMapper(),
new DiscriminatedUnionShapeId()
new DiscriminatedUnionShapeId(),
new JsonUnknownMapper()
).asJava

}
50 changes: 50 additions & 0 deletions modules/openapi/src/alloy/openapi/JsonUnknownMapper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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 software.amazon.smithy.jsonschema.JsonSchemaMapper
import software.amazon.smithy.jsonschema.JsonSchemaConfig
import software.amazon.smithy.jsonschema.Schema.Builder
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.Shape
import alloy.JsonUnknownTrait

import scala.jdk.CollectionConverters._

class JsonUnknownMapper() extends JsonSchemaMapper {
private final val ADDITIONAL_PROPERTIES = "additionalProperties"

override def updateSchema(
shape: Shape,
schemaBuilder: Builder,
config: JsonSchemaConfig
): Builder = {
val jsonUnknownMemberName = shape
.getAllMembers()
.asScala
.collect {
case (name, member) if member.hasTrait(classOf[JsonUnknownTrait]) =>
name
}
.headOption

jsonUnknownMemberName.fold(schemaBuilder) { name =>
schemaBuilder
.removeProperty(name)
.putExtension(ADDITIONAL_PROPERTIES, Node.from(true))
}
}
}
1 change: 1 addition & 0 deletions modules/openapi/test/resources/foo.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
"type": "string"
}
},
"additionalProperties": true,
"example": {
"name": "Woof"
}
Expand Down
8 changes: 8 additions & 0 deletions modules/openapi/test/resources/foo.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use alloy#discriminated
use alloy#nullable
use alloy#untagged
use alloy#dataExamples
use alloy#jsonUnknown

@simpleRestJson
@externalDocumentation(
Expand Down Expand Up @@ -164,6 +165,13 @@ structure Cat {
structure Dog {
name: String,
breed: String
@jsonUnknown
attributes: Attributes
}

map Attributes {
key: String
value: Document
}

@dataExamples([{
Expand Down
Loading