Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 6, 2024
1 parent 634fb00 commit a62473b
Show file tree
Hide file tree
Showing 30 changed files with 322 additions and 871 deletions.
3 changes: 3 additions & 0 deletions src/Parser/Popo/Resolver/Documentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ private function buildPropertyType(Type $type): ?PropertyTypeAbstract
return PropertyTypeFactory::getAny();
} elseif ($type instanceof Types\Nullable) {
return $this->buildPropertyType($type->getActualType());
} elseif ($type instanceof Types\Compound) {
// compound types are only used i.e. array<string>|null so we always use the first type
return $this->buildPropertyType($type->get(0));
}

return null;
Expand Down
24 changes: 23 additions & 1 deletion src/Parser/TypeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PSX\Schema\Definitions;
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Exception\ParserException;
use PSX\Schema\Exception\TypeNotFoundException;
use PSX\Schema\Exception\UnknownTypeException;
use PSX\Schema\Format;
use PSX\Schema\Parser\TypeSchema\BCLayer;
Expand Down Expand Up @@ -76,6 +77,14 @@ public function parseSchema(\stdClass $data, ?ContextInterface $context = null):
$this->parseDefinitions($data, $definitions);

$root = $this->getStringValue($data, ['root', '$ref']);
if (empty($root)) {
try {
$type = $this->parseDefinitionType($data);
$root = 'RootType';
$definitions->addType($root, $type);
} catch (UnknownTypeException) {
}
}

return new Schema($definitions, $root);
}
Expand Down Expand Up @@ -132,6 +141,9 @@ private function parseImport(\stdClass $schema, DefinitionsInterface $definition
}
}

/**
* @throws UnknownTypeException
*/
public function parseDefinitionType(\stdClass $data, ?string $namespace = null): Type\DefinitionTypeAbstract
{
$data = BCLayer::transformDefinition($data);
Expand All @@ -157,6 +169,8 @@ public function parseDefinitionType(\stdClass $data, ?string $namespace = null):
$this->parseDefinitionStruct($type, $data, $namespace);
} elseif ($type instanceof Type\MapDefinitionType) {
$this->parseDefinitionMap($type, $data, $namespace);
} elseif ($type instanceof Type\ArrayDefinitionType) {
$this->parseDefinitionArray($type, $data, $namespace);
}

return $type;
Expand Down Expand Up @@ -223,7 +237,15 @@ private function parseDefinitionStruct(StructDefinitionType $type, \stdClass $da

private function parseDefinitionMap(Type\MapDefinitionType $type, \stdClass $data, ?string $namespace): void
{
$schema = $this->getObjectValue($data, ['schema']);
$schema = $this->getObjectValue($data, ['schema', 'additionalProperties']);
if ($schema instanceof \stdClass) {
$type->setSchema($this->parsePropertyType($schema, $namespace));
}
}

private function parseDefinitionArray(Type\ArrayDefinitionType $type, \stdClass $data, ?string $namespace): void
{
$schema = $this->getObjectValue($data, ['schema', 'items']);
if ($schema instanceof \stdClass) {
$type->setSchema($this->parsePropertyType($schema, $namespace));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Type/StructDefinitionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public function toArray(): array
'base' => $this->base,
'properties' => $this->properties,
'discriminator' => $this->discriminator,
'mapping' => $this->mapping,
'template' => $this->template,
'mapping' => !empty($this->mapping) ? $this->mapping : null,
'template' => !empty($this->template) ? $this->template : null,
], function($value){
return $value !== null;
}));
Expand Down
15 changes: 0 additions & 15 deletions tests/Generator/resource/csharp/csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@ public class Location

using System.Text.Json.Serialization;

/// <summary>
/// An application
/// </summary>
public class Web
{
[JsonPropertyName("name")]
public string? Name { get; set; }

[JsonPropertyName("url")]
public string? Url { get; set; }

}

using System.Text.Json.Serialization;

/// <summary>
/// An simple author element with some description
/// </summary>
Expand Down
6 changes: 0 additions & 6 deletions tests/Generator/resource/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ type Location struct {
Long float64 `json:"long"`
}

// An application
type Web struct {
Name string `json:"name"`
Url string `json:"url"`
}

// An simple author element with some description
type Author struct {
Title string `json:"title"`
Expand Down
5 changes: 0 additions & 5 deletions tests/Generator/resource/graphql/graphql.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ type Location {
long: Float
}

type Web {
name: String
url: String
}

type Author {
title: String
email: String
Expand Down
5 changes: 0 additions & 5 deletions tests/Generator/resource/html/html.htm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
<span class="psx-object-json-key">"long"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Number</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">lat</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Number">Number</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">long</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Number">Number</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>

<div id="Web" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Web">Web</a></h1><div class="psx-object-description">An application</div><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"name"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-key">"url"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">name</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="String">String</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">url</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="String">String</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>

<div id="Author" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Author">Author</a></h1><div class="psx-object-description">An simple author element with some description</div><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"title"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-key">"email"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
Expand Down
31 changes: 0 additions & 31 deletions tests/Generator/resource/java/java.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,6 @@ public Double getLong() {
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

/**
* An application
*/
public class Web {
private String name;
private String url;

@JsonSetter("name")
public void setName(String name) {
this.name = name;
}

@JsonGetter("name")
public String getName() {
return this.name;
}

@JsonSetter("url")
public void setUrl(String url) {
this.url = url;
}

@JsonGetter("url")
public String getUrl() {
return this.url;
}
}

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

/**
* An simple author element with some description
*/
Expand Down
12 changes: 0 additions & 12 deletions tests/Generator/resource/jsonschema/jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@
},
"payload": []
}
},
"Web": {
"description": "An application",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
},
"description": "An general news entry",
Expand Down
8 changes: 0 additions & 8 deletions tests/Generator/resource/kotlin/kotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ open class Location {
var long: Float? = null
}

/**
* An application
*/
open class Web {
var name: String? = null
var url: String? = null
}

/**
* An simple author element with some description
*/
Expand Down
9 changes: 0 additions & 9 deletions tests/Generator/resource/markdown/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ Field | Type | Description
lat | Number |
long | Number |

# Web

An application

Field | Type | Description
----- | ---- | -----------
name | String |
url | String |

# Author

An simple author element with some description
Expand Down
37 changes: 0 additions & 37 deletions tests/Generator/resource/php/php.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,6 @@ public function jsonSerialize() : object
}
}

use PSX\Schema\Attribute\Description;

#[Description('An application')]
class Web implements \JsonSerializable, \PSX\Record\RecordableInterface
{
protected ?string $name;
protected ?string $url;
public function setName(?string $name) : void
{
$this->name = $name;
}
public function getName() : ?string
{
return $this->name;
}
public function setUrl(?string $url) : void
{
$this->url = $url;
}
public function getUrl() : ?string
{
return $this->url;
}
public function toRecord() : \PSX\Record\RecordInterface
{
/** @var \PSX\Record\Record<mixed> $record */
$record = new \PSX\Record\Record();
$record->put('name', $this->name);
$record->put('url', $this->url);
return $record;
}
public function jsonSerialize() : object
{
return (object) $this->toRecord()->getAll();
}
}

use PSX\Schema\Attribute\Description;
use PSX\Schema\Attribute\Nullable;

Expand Down
5 changes: 0 additions & 5 deletions tests/Generator/resource/protobuf/protobuf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ message Location {
optional float long = 2 [json_name="long"];
}

message Web {
optional string name = 1 [json_name="name"];
optional string url = 2 [json_name="url"];
}

message Author {
optional string title = 1 [json_name="title"];
optional string email = 2 [json_name="email"];
Expand Down
11 changes: 0 additions & 11 deletions tests/Generator/resource/python/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ class Location(BaseModel):
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union, UserList, UserDict


# An application
class Web(BaseModel):
name: Optional[str] = Field(default=None, alias="name")
url: Optional[str] = Field(default=None, alias="url")
pass

from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union, UserList, UserDict


# An simple author element with some description
class Author(BaseModel):
title: Optional[str] = Field(default=None, alias="title")
Expand Down
10 changes: 0 additions & 10 deletions tests/Generator/resource/ruby/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ def initialize(lat, long)
end
end

# An application
class Web
attr_accessor :name, :url

def initialize(name, url)
@name = name
@url = url
end
end

# An simple author element with some description
class Author
attr_accessor :title, :email, :categories, :locations, :origin
Expand Down
13 changes: 0 additions & 13 deletions tests/Generator/resource/rust/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ pub struct Location {

use serde::{Serialize, Deserialize};

// An application
#[derive(Serialize, Deserialize)]
pub struct Web {
#[serde(rename = "name")]
name: Option<String>,

#[serde(rename = "url")]
url: Option<String>,

}

use serde::{Serialize, Deserialize};

// An simple author element with some description
#[derive(Serialize, Deserialize)]
pub struct Author {
Expand Down
11 changes: 0 additions & 11 deletions tests/Generator/resource/swift/swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ class Location: Codable {
}
}

// An application
class Web: Codable {
var name: String
var url: String

enum CodingKeys: String, CodingKey {
case name = "name"
case url = "url"
}
}

// An simple author element with some description
class Author: Codable {
var title: String
Expand Down
12 changes: 0 additions & 12 deletions tests/Generator/resource/typeschema/typeschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,6 @@
"type": "any"
}
}
},
"Web": {
"description": "An application",
"type": "struct",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
},
"root": "News"
Expand Down
8 changes: 0 additions & 8 deletions tests/Generator/resource/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ export class Location {
long?: number
}

/**
* An application
*/
export class Web {
name?: string
url?: string
}

import {Location} from "./Location";

/**
Expand Down
Loading

0 comments on commit a62473b

Please sign in to comment.