Skip to content

Commit

Permalink
Merge pull request #183 from benoitlouy/jsonunknown-additionalprop
Browse files Browse the repository at this point in the history
update openapi/jsonschema conversion to translate jsonUnknown to additionalProperties
  • Loading branch information
lewisjkl authored Aug 14, 2024
2 parents 5cc8125 + 02adbac commit 214146b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
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

0 comments on commit 214146b

Please sign in to comment.