Skip to content

Commit

Permalink
fix javascript self import
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Mar 10, 2024
1 parent e4f160d commit aac53eb
Show file tree
Hide file tree
Showing 33 changed files with 122 additions and 57 deletions.
7 changes: 1 addition & 6 deletions src/Generator/CSharp.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return $code;
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand All @@ -125,11 +125,6 @@ protected function writeHeader(TypeAbstract $origin): string
return $code;
}

protected function writeFooter(TypeAbstract $origin): string
{
return '';
}

private function getImports(TypeAbstract $origin): array
{
$imports = [];
Expand Down
34 changes: 17 additions & 17 deletions src/Generator/CodeGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private function generateDefinition(string $name, TypeInterface $type)
}
}

private function generateStruct(string $className, StructType $type)
private function generateStruct(string $className, StructType $type): void
{
$properties = [];

Expand Down Expand Up @@ -195,57 +195,57 @@ private function generateStruct(string $className, StructType $type)
$code = $this->writeStruct($className, $props, $extends, $generics, $type);

if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

private function generateMap(string $className, MapType $type)
private function generateMap(string $className, MapType $type): void
{
$className = new Code\Name($className, $className, $this->normalizer);

$code = $this->writeMap($className, $this->generator->getType($type), $type);
if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

private function generateArray(string $className, ArrayType $type)
private function generateArray(string $className, ArrayType $type): void
{
$className = new Code\Name($className, $className, $this->normalizer);

$code = $this->writeArray($className, $this->generator->getType($type), $type);
if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

private function generateUnion(string $className, UnionType $type)
private function generateUnion(string $className, UnionType $type): void
{
$className = new Code\Name($className, $className, $this->normalizer);

$code = $this->writeUnion($className, $this->generator->getType($type), $type);
if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

private function generateIntersection(string $className, IntersectionType $type)
private function generateIntersection(string $className, IntersectionType $type): void
{
$className = new Code\Name($className, $className, $this->normalizer);

$code = $this->writeIntersection($className, $this->generator->getType($type), $type);
if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

private function generateReference(string $className, ReferenceType $type)
private function generateReference(string $className, ReferenceType $type): void
{
$className = new Code\Name($className, $className, $this->normalizer);

$code = $this->writeReference($className, $this->generator->getType($type), $type);
if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type));
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
}
}

Expand Down Expand Up @@ -284,12 +284,12 @@ private function getGeneric(TypeInterface $type): ?GenericType
}
}

private function wrap(string $code, TypeAbstract $type): string
private function wrap(string $code, TypeAbstract $type, Code\Name $className): string
{
return implode("\n", array_filter(array_map('trim', [
$this->writeHeader($type),
$this->writeHeader($type, $className),
$code,
$this->writeFooter($type)
$this->writeFooter($type, $className)
]))) . "\n";
}

Expand Down Expand Up @@ -337,12 +337,12 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return '';
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
return '';
}

protected function writeFooter(TypeAbstract $origin): string
protected function writeFooter(TypeAbstract $origin, Code\Name $className): string
{
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Go.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return 'type ' . $name->getClass() . ' = ' . $type . "\n";
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = "\n";

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Java.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return $code;
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return 'typealias ' . $name->getClass() . ' = ' . $type . "\n";
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return $code;
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Ruby.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return $code;
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand All @@ -109,7 +109,7 @@ protected function writeHeader(TypeAbstract $origin): string
return $code;
}

protected function writeFooter(TypeAbstract $origin): string
protected function writeFooter(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Rust.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return 'pub type ' . $name->getClass() . ' = ' . $type . ';' . "\n";
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return 'typealias ' . $name->getClass() . ' = ' . $type . ';' . "\n";
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand Down
17 changes: 12 additions & 5 deletions src/Generator/TypeScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return 'export type ' . $name->getClass() . ' = ' . $type . ';' . "\n";
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

$imports = $this->getImports($origin);
$imports = $this->getImports($origin, $className);
if (!empty($imports)) {
$code.= "\n";
$code.= implode("\n", $imports);
Expand All @@ -135,10 +135,10 @@ protected function writeHeader(TypeAbstract $origin): string
return $code;
}

private function getImports(TypeInterface $origin): array
private function getImports(TypeInterface $origin, Code\Name $className): array
{
$refs = [];
TypeUtil::walk($origin, function(TypeInterface $type) use (&$refs){
TypeUtil::walk($origin, function(TypeInterface $type) use (&$refs, $className){
if ($type instanceof ReferenceType) {
$refs[$type->getRef()] = $type->getRef();
if ($type->getTemplate()) {
Expand All @@ -154,7 +154,14 @@ private function getImports(TypeInterface $origin): array
$imports = [];
foreach ($refs as $ref) {
[$ns, $name] = TypeUtil::split($ref);
$imports[] = 'import {' . $this->normalizer->class($name) . '} from "./' . $this->normalizer->file($name) . '";';

$typeName = $this->normalizer->class($name);
if ($typeName === $className->getClass()) {
// we dont need to include the same class
continue;
}

$imports[] = 'import {' . $typeName . '} from "./' . $this->normalizer->file($name) . '";';
}

return $imports;
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/VisualBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function writeReference(Code\Name $name, string $type, ReferenceType $
return $code;
}

protected function writeHeader(TypeAbstract $origin): string
protected function writeHeader(TypeAbstract $origin, Code\Name $className): string
{
$code = '';

Expand All @@ -122,7 +122,7 @@ protected function writeHeader(TypeAbstract $origin): string
return $code;
}

protected function writeFooter(TypeAbstract $origin): string
protected function writeFooter(TypeAbstract $origin, Code\Name $className): string
{
if (!empty($this->namespace)) {
return 'End Namespace' . "\n";
Expand Down
2 changes: 2 additions & 0 deletions tests/Generator/resource/csharp/csharp_oop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ public class Human
{
[JsonPropertyName("firstName")]
public string? FirstName { get; set; }
[JsonPropertyName("parent")]
public Human? Parent { get; set; }
}

using System.Text.Json.Serialization;
Expand Down
1 change: 1 addition & 0 deletions tests/Generator/resource/go/go_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Import struct {
type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Parent *Human `json:"parent"`
}
1 change: 1 addition & 0 deletions tests/Generator/resource/go/go_import_ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ package Foo.Bar
type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Parent *My.Import.Human `json:"parent"`
}
2 changes: 2 additions & 0 deletions tests/Generator/resource/go/go_oop.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
type Human struct {
FirstName string `json:"firstName"`
Parent *Human `json:"parent"`
}

type Student struct {
FirstName string `json:"firstName"`
Parent *Human `json:"parent"`
MatricleNumber string `json:"matricleNumber"`
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/resource/html/html_oop.htm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div id="Human" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Human">Human</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"firstName"</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 psx-property-optional">firstName</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>
<span class="psx-object-json-key">"parent"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Human</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 psx-property-optional">firstName</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 psx-property-optional">parent</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Human">Human</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>

<div id="Student" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Student">Student</a> extends <a class="psx-type-link" data-name="Human">Human</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"matricleNumber"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
Expand Down
9 changes: 9 additions & 0 deletions tests/Generator/resource/java/java_oop.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
public class Human {
private String firstName;
private Human parent;
@JsonSetter("firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
Expand All @@ -11,6 +12,14 @@ public void setFirstName(String firstName) {
public String getFirstName() {
return this.firstName;
}
@JsonSetter("parent")
public void setParent(Human parent) {
this.parent = parent;
}
@JsonGetter("parent")
public Human getParent() {
return this.parent;
}
}

import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down
19 changes: 11 additions & 8 deletions tests/Generator/resource/jsonschema/jsonschema_import.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"properties": {
"firstName": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/Human"
}
}
},
Expand All @@ -24,14 +27,14 @@
"type": "object",
"properties": {
"students": {
"$ref": "#\/definitions\/StudentMap"
"$ref": "#/definitions/StudentMap"
}
}
},
"Student": {
"allOf": [
{
"$ref": "#\/definitions\/Human"
"$ref": "#/definitions/Human"
},
{
"type": "object",
Expand All @@ -54,7 +57,7 @@
"items": {
"allOf": [
{
"$ref": "#\/definitions\/Human"
"$ref": "#/definitions/Human"
},
{
"type": "object",
Expand All @@ -73,23 +76,23 @@
"type": "object",
"properties": {
"students": {
"$ref": "#\/definitions\/StudentMap"
"$ref": "#/definitions/StudentMap"
},
"student": {
"$ref": "#\/definitions\/Student"
"$ref": "#/definitions/Student"
}
}
},
"MyMap": {
"allOf": [
{
"$ref": "#\/definitions\/Student"
"$ref": "#/definitions/Student"
},
{
"type": "object"
}
]
}
},
"$ref": "#\/definitions\/Import"
}
"$ref": "#/definitions/Import"
}
Loading

0 comments on commit aac53eb

Please sign in to comment.