Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 @@ -286,7 +286,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
let dependencies: [Decl]

switch schema {
case .object, .one:
case .object, .one, .any:
let structureGen = try StructureSwiftGen(
swiftTypeName: typeCased(name),
structure: schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ public struct StructureSwiftGen: JSONSchemaSwiftGenerator {
rootConformances: rootConformances
)
]
case .one(of: let schemas, core: _):
// NOTE: This is not a great place to treat "anyOf" the same way as
// "oneOf" but doing so might successfully parse a specific
// subset of "anyOf"s: those where you can expect one of the
// cases to succeed completely but just happen to have overlapping
// success cases -- even then, you may end up with a Poly that
// successfully parses fewer than all of an encoded resource because
// of two applicable "anyOf" branches the less inclusive one came first.
case .one(of: let schemas, core: _), .any(of: let schemas, core: _):
let poly = try StructureSwiftGen.structure(
named: typeName,
forOneOf: schemas,
Expand Down
128 changes: 128 additions & 0 deletions 128 Tests/JSONAPISwiftGenTests/ResourceObjectSwiftGenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class ResourceObjectSwiftGenTests: XCTestCase {
}

func test_polyAttribute() throws {
// test oneOf in simplest case
let openAPIStructure = try testDecoder.decode(
JSONSchema.self,
from: """
Expand Down Expand Up @@ -109,6 +110,133 @@ class ResourceObjectSwiftGenTests: XCTestCase {

print(polyAttrSwiftGen.swiftCode)
}

func test_polyAttribute2() throws {
// test oneOf with type & nullable at root
let openAPIStructure = try testDecoder.decode(
JSONSchema.self,
from: """
{
"type": "object",
"properties": {
"type": {"type": "string", "enum": ["poly_thing"]},
"id": {"type": "string"},
"attributes": {
"type": "object",
"properties": {
"poly_property": {
"type": "object",
"nullable": true,
"oneOf": [
{
"type": "object",
"title": "Widget",
"additionalProperties": false,
"nullable": true,
"required": [
"prop"
],
"properties": {
"prop": {
"type": "string",
"enum": [
"yes",
"no"
]
},
"reasoning": {
"type": "string",
"nullable": true
}
}
},
{
"type": "object",
"title": "Cog",
"additionalProperties": false,
"required": [
"built"
],
"properties": {
"built": {
"type": "boolean"
}
}
}
]
}
}
}
}
}
""".data(using: .utf8)!
).dereferenced()!

let polyAttrSwiftGen = try ResourceObjectSwiftGen(structure: openAPIStructure)

XCTAssertEqual(polyAttrSwiftGen.resourceTypeName, "PolyThing")

print(polyAttrSwiftGen.swiftCode)
}

func test_polyAttribute3() throws {
// test anyOf as Poly
let openAPIStructure = try testDecoder.decode(
JSONSchema.self,
from: """
{
"type": "object",
"properties": {
"type": {"type": "string", "enum": ["poly_thing"]},
"id": {"type": "string"},
"attributes": {
"type": "object",
"properties": {
"poly_property": {
"type": "object",
"nullable": true,
"anyOf": [
{
"type": "object",
"title": "Metadata 1",
"additionalProperties": true,
"nullable": true,
"properties": {
"title": {
"type": "string",
"description": "title"
}
}
},
{
"type": "object",
"title": "Metadata 2",
"additionalProperties": true,
"properties": {
"title": {
"type": "string",
"description": "title"
},
"is_starred": {
"type": "boolean"
}
}
}
]
}
}
}
}
}
""".data(using: .utf8)!
).dereferenced()!

let polyAttrSwiftGen = try ResourceObjectSwiftGen(structure: openAPIStructure)

XCTAssertEqual(polyAttrSwiftGen.resourceTypeName, "PolyThing")

print(polyAttrSwiftGen.swiftCode)
}
}

enum TestPersonDescription: JSONAPI.ResourceObjectDescription {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.