Skip to content

Commit

Permalink
update complex schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 17, 2024
1 parent 089e444 commit e69fae6
Show file tree
Hide file tree
Showing 105 changed files with 963 additions and 1,461 deletions.
33 changes: 32 additions & 1 deletion src/Generator/CSharp.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

namespace PSX\Schema\Generator;

use PSX\Schema\Exception\TypeNotFoundException;
use PSX\Schema\Generator\Normalizer\NormalizerInterface;
use PSX\Schema\Generator\Type\GeneratorInterface;
use PSX\Schema\Type\ArrayDefinitionType;
use PSX\Schema\Type\DefinitionTypeAbstract;
use PSX\Schema\Type\MapDefinitionType;
use PSX\Schema\Type\ReferencePropertyType;
use PSX\Schema\Type\StructDefinitionType;
use PSX\Schema\TypeUtil;

Expand Down Expand Up @@ -86,8 +88,15 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte

foreach ($properties as $property) {
/** @var Code\Property $property */
// in case we override an existing property we need to use the new keyword s.
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-modifier
$override = '';
if ($this->hasPropertyInParent($property->getName()->getRaw(), $origin->getParent())) {
$override = 'new ';
}

$code.= $this->indent . '[JsonPropertyName("' . $property->getName()->getRaw() . '")]' . "\n";
$code.= $this->indent . 'public ' . $property->getType() . '? ' . $property->getName()->getProperty() . ' { get; set; }' . "\n";
$code.= $this->indent . 'public ' . $override . $property->getType() . '? ' . $property->getName()->getProperty() . ' { get; set; }' . "\n";
$code.= "\n";
}

Expand Down Expand Up @@ -151,4 +160,26 @@ private function getImports(DefinitionTypeAbstract $origin): array

return $imports;
}

/**
* @throws TypeNotFoundException
*/
private function hasPropertyInParent(string $propertyName, ?ReferencePropertyType $reference): bool
{
$target = $reference?->getTarget();
if (empty($target)) {
return false;
}

$parent = $this->definitions->getType($target);
if (!$parent instanceof StructDefinitionType) {
return false;
}

if ($parent->hasProperty($propertyName)) {
return true;
}

return $this->hasPropertyInParent($propertyName, $parent->getParent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace TypeAPI.Model;
/// Base collection type
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(MapDefinitionType), typeDiscriminator: "map")]
[JsonDerivedType(typeof(ArrayDefinitionType), typeDiscriminator: "array")]
[JsonDerivedType(typeof(MapDefinitionType), typeDiscriminator: "map")]
public abstract class CollectionDefinitionType : DefinitionType
{
[JsonPropertyName("schema")]
public PropertyType? Schema { get; set; }

[JsonPropertyName("type")]
public new string? Type { get; set; }

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ namespace TypeAPI.Model;
/// <summary>
/// Base collection property type
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(ArrayPropertyType), typeDiscriminator: "array")]
[JsonDerivedType(typeof(MapPropertyType), typeDiscriminator: "map")]
public abstract class CollectionPropertyType : PropertyType
{
[JsonPropertyName("schema")]
public PropertyType? Schema { get; set; }

[JsonPropertyName("type")]
public new string? Type { get; set; }

}

10 changes: 5 additions & 5 deletions tests/Generator/resource/csharp/complex/DefinitionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace TypeAPI.Model;
/// Base definition type
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(StructDefinitionType), typeDiscriminator: "struct")]
[JsonDerivedType(typeof(MapDefinitionType), typeDiscriminator: "map")]
[JsonDerivedType(typeof(ArrayDefinitionType), typeDiscriminator: "array")]
[JsonDerivedType(typeof(MapDefinitionType), typeDiscriminator: "map")]
[JsonDerivedType(typeof(StructDefinitionType), typeDiscriminator: "struct")]
public abstract class DefinitionType
{
[JsonPropertyName("deprecated")]
public bool? Deprecated { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("deprecated")]
public bool? Deprecated { get; set; }

}

24 changes: 12 additions & 12 deletions tests/Generator/resource/csharp/complex/PropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ namespace TypeAPI.Model;
/// Base property type
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(StringPropertyType), typeDiscriminator: "string")]
[JsonDerivedType(typeof(IntegerPropertyType), typeDiscriminator: "integer")]
[JsonDerivedType(typeof(NumberPropertyType), typeDiscriminator: "number")]
[JsonDerivedType(typeof(BooleanPropertyType), typeDiscriminator: "boolean")]
[JsonDerivedType(typeof(MapPropertyType), typeDiscriminator: "map")]
[JsonDerivedType(typeof(ArrayPropertyType), typeDiscriminator: "array")]
[JsonDerivedType(typeof(AnyPropertyType), typeDiscriminator: "any")]
[JsonDerivedType(typeof(ArrayPropertyType), typeDiscriminator: "array")]
[JsonDerivedType(typeof(BooleanPropertyType), typeDiscriminator: "boolean")]
[JsonDerivedType(typeof(GenericPropertyType), typeDiscriminator: "generic")]
[JsonDerivedType(typeof(IntegerPropertyType), typeDiscriminator: "integer")]
[JsonDerivedType(typeof(MapPropertyType), typeDiscriminator: "map")]
[JsonDerivedType(typeof(NumberPropertyType), typeDiscriminator: "number")]
[JsonDerivedType(typeof(ReferencePropertyType), typeDiscriminator: "reference")]
[JsonDerivedType(typeof(StringPropertyType), typeDiscriminator: "string")]
public abstract class PropertyType
{
[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("deprecated")]
public bool? Deprecated { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("nullable")]
public bool? Nullable { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

}

7 changes: 5 additions & 2 deletions tests/Generator/resource/csharp/complex/ScalarPropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ namespace TypeAPI.Model;
/// Base scalar property type
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(StringPropertyType), typeDiscriminator: "string")]
[JsonDerivedType(typeof(BooleanPropertyType), typeDiscriminator: "boolean")]
[JsonDerivedType(typeof(IntegerPropertyType), typeDiscriminator: "integer")]
[JsonDerivedType(typeof(NumberPropertyType), typeDiscriminator: "number")]
[JsonDerivedType(typeof(BooleanPropertyType), typeDiscriminator: "boolean")]
[JsonDerivedType(typeof(StringPropertyType), typeDiscriminator: "string")]
public abstract class ScalarPropertyType : PropertyType
{
[JsonPropertyName("type")]
public new string? Type { get; set; }

}

12 changes: 6 additions & 6 deletions tests/Generator/resource/csharp/complex/StructDefinitionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace TypeAPI.Model;
/// </summary>
public class StructDefinitionType : DefinitionType
{
[JsonPropertyName("parent")]
public ReferencePropertyType? Parent { get; set; }

[JsonPropertyName("base")]
public bool? Base { get; set; }

[JsonPropertyName("properties")]
public System.Collections.Generic.Dictionary<string, PropertyType>? Properties { get; set; }

[JsonPropertyName("discriminator")]
public string? Discriminator { get; set; }

[JsonPropertyName("mapping")]
public System.Collections.Generic.Dictionary<string, string>? Mapping { get; set; }

[JsonPropertyName("parent")]
public ReferencePropertyType? Parent { get; set; }

[JsonPropertyName("properties")]
public System.Collections.Generic.Dictionary<string, PropertyType>? Properties { get; set; }

}

6 changes: 3 additions & 3 deletions tests/Generator/resource/csharp/complex/TypeSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace TypeAPI.Model;
/// </summary>
public class TypeSchema
{
[JsonPropertyName("import")]
public System.Collections.Generic.Dictionary<string, string>? Import { get; set; }

[JsonPropertyName("definitions")]
public System.Collections.Generic.Dictionary<string, DefinitionType>? Definitions { get; set; }

[JsonPropertyName("import")]
public System.Collections.Generic.Dictionary<string, string>? Import { get; set; }

[JsonPropertyName("root")]
public string? Root { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/any_property_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents an any value which allows any kind of value
type AnyPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
}

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/array_definition_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents an array which contains a dynamic list of values of the same type
type ArrayDefinitionType struct {
Description string `json:"description"`
Schema *PropertyType `json:"schema"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Schema *PropertyType `json:"schema"`
Description string `json:"description"`
}

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/array_property_type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// Represents an array which contains a dynamic list of values of the same type
type ArrayPropertyType struct {
Description string `json:"description"`
Schema *PropertyType `json:"schema"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Schema *PropertyType `json:"schema"`
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents a boolean value
type BooleanPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Base collection type
type CollectionDefinitionType struct {
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Schema *PropertyType `json:"schema"`
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// Base collection property type
type CollectionPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
Schema *PropertyType `json:"schema"`
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/complex/definition_type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

// Base definition type
type DefinitionType struct {
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
}

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/generic_property_type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// Represents a generic value which can be replaced with a dynamic type
type GenericPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
Name string `json:"name"`
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents an integer value
type IntegerPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/complex/map_definition_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents a map which contains a dynamic set of key value entries of the same type
type MapDefinitionType struct {
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Schema *PropertyType `json:"schema"`
}

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/map_property_type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// Represents a map which contains a dynamic set of key value entries of the same type
type MapPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
Schema *PropertyType `json:"schema"`
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Represents a float value
type NumberPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
}

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/property_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Base property type
type PropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// Represents a reference to a definition type
type ReferencePropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
Target string `json:"target"`
Template map[string]string `json:"template"`
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/go/complex/scalar_property_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Base scalar property type
type ScalarPropertyType struct {
Description string `json:"description"`
Type string `json:"type"`
Deprecated bool `json:"deprecated"`
Description string `json:"description"`
Nullable bool `json:"nullable"`
Type string `json:"type"`
}

Loading

0 comments on commit e69fae6

Please sign in to comment.