Skip to content

Commit

Permalink
use json class and property description annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 29, 2024
1 parent 271a659 commit 40d639e
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 71 deletions.
18 changes: 10 additions & 8 deletions src/Generator/Java.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte
$code.= '})' . "\n";
}

$comment = $origin->getDescription();
if (!empty($comment)) {
$code.= '@JsonClassDescription("' . $comment . '")' . "\n";
}

$code.= 'public ' . ($origin->getBase() === true ? 'abstract ' : '') . 'class ' . $name->getClass();

if (!empty($generics)) {
Expand All @@ -86,6 +91,11 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte

foreach ($properties as $property) {
/** @var Code\Property $property */
$comment = $property->getComment();
if (!empty($comment)) {
$code.= $this->indent . '@JsonPropertyDescription("' . $comment . '")' . "\n";
}

$code.= $this->indent . 'private ' . $property->getType() . ' ' . $property->getName()->getProperty() . ';' . "\n";
}

Expand Down Expand Up @@ -139,14 +149,6 @@ protected function writeHeader(DefinitionTypeAbstract $origin, Code\Name $classN
$code.= "\n";
}

$comment = $origin->getDescription();
if (!empty($comment)) {
$code.= "\n";
$code.= '/**' . "\n";
$code.= ' * ' . $comment . "\n";
$code.= ' */';
}

return $code;
}

Expand Down
4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/AnyPropertyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents an any value which allows any kind of value
*/
@JsonClassDescription("Represents an any value which allows any kind of value")
public class AnyPropertyType extends PropertyType {
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents an array which contains a dynamic list of values of the same type
*/
@JsonClassDescription("Represents an array which contains a dynamic list of values of the same type")
public class ArrayDefinitionType extends CollectionDefinitionType {
}

4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/ArrayPropertyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents an array which contains a dynamic list of values of the same type
*/
@JsonClassDescription("Represents an array which contains a dynamic list of values of the same type")
public class ArrayPropertyType extends CollectionPropertyType {
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a boolean value
*/
@JsonClassDescription("Represents a boolean value")
public class BooleanPropertyType extends ScalarPropertyType {
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.fasterxml.jackson.annotation.*;

/**
* Base collection type
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ArrayDefinitionType.class, name = "array"),
@JsonSubTypes.Type(value = MapDefinitionType.class, name = "map"),
})
@JsonClassDescription("Base collection type")
public abstract class CollectionDefinitionType extends DefinitionType {
private PropertyType schema;
private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.fasterxml.jackson.annotation.*;

/**
* Base collection property type
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ArrayPropertyType.class, name = "array"),
@JsonSubTypes.Type(value = MapPropertyType.class, name = "map"),
})
@JsonClassDescription("Base collection property type")
public abstract class CollectionPropertyType extends PropertyType {
private PropertyType schema;
private String type;
Expand Down
4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/DefinitionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.fasterxml.jackson.annotation.*;

/**
* Base definition type
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ArrayDefinitionType.class, name = "array"),
@JsonSubTypes.Type(value = MapDefinitionType.class, name = "map"),
@JsonSubTypes.Type(value = StructDefinitionType.class, name = "struct"),
})
@JsonClassDescription("Base definition type")
public abstract class DefinitionType {
private Boolean deprecated;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a generic value which can be replaced with a dynamic type
*/
@JsonClassDescription("Represents a generic value which can be replaced with a dynamic type")
public class GenericPropertyType extends PropertyType {
@JsonPropertyDescription("The name of the generic, it is recommended to use common generic names like T or TValue. These generics can then be replaced on usage with a concrete type through the template property at a reference.")
private String name;

@JsonSetter("name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents an integer value
*/
@JsonClassDescription("Represents an integer value")
public class IntegerPropertyType extends ScalarPropertyType {
}

4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/MapDefinitionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a map which contains a dynamic set of key value entries of the same type
*/
@JsonClassDescription("Represents a map which contains a dynamic set of key value entries of the same type")
public class MapDefinitionType extends CollectionDefinitionType {
}

4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/MapPropertyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a map which contains a dynamic set of key value entries of the same type
*/
@JsonClassDescription("Represents a map which contains a dynamic set of key value entries of the same type")
public class MapPropertyType extends CollectionPropertyType {
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a float value
*/
@JsonClassDescription("Represents a float value")
public class NumberPropertyType extends ScalarPropertyType {
}

4 changes: 1 addition & 3 deletions tests/Generator/resource/java/complex/PropertyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.fasterxml.jackson.annotation.*;

/**
* Base property type
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = AnyPropertyType.class, name = "any"),
Expand All @@ -17,6 +14,7 @@
@JsonSubTypes.Type(value = ReferencePropertyType.class, name = "reference"),
@JsonSubTypes.Type(value = StringPropertyType.class, name = "string"),
})
@JsonClassDescription("Base property type")
public abstract class PropertyType {
private Boolean deprecated;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a reference to a definition type
*/
@JsonClassDescription("Represents a reference to a definition type")
public class ReferencePropertyType extends PropertyType {
@JsonPropertyDescription("The target type, this must be a key which is available under the definitions keyword.")
private String target;
@JsonPropertyDescription("A map where the key is the name of the generic and the value must point to a key under the definitions keyword. This can be used in case the target points to a type which contains generics, then it is possible to replace those generics with a concrete type.")
private java.util.Map<String, String> template;

@JsonSetter("target")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import com.fasterxml.jackson.annotation.*;

/**
* Base scalar property type
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = BooleanPropertyType.class, name = "boolean"),
@JsonSubTypes.Type(value = IntegerPropertyType.class, name = "integer"),
@JsonSubTypes.Type(value = NumberPropertyType.class, name = "number"),
@JsonSubTypes.Type(value = StringPropertyType.class, name = "string"),
})
@JsonClassDescription("Base scalar property type")
public abstract class ScalarPropertyType extends PropertyType {
private String type;

Expand Down
5 changes: 2 additions & 3 deletions tests/Generator/resource/java/complex/StringPropertyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.fasterxml.jackson.annotation.*;

/**
* Represents a string value
*/
@JsonClassDescription("Represents a string value")
public class StringPropertyType extends ScalarPropertyType {
@JsonPropertyDescription("Optional describes the format of the string. Supported are the following types: date, date-time and time. A code generator may use a fitting data type to represent such a format, if not supported it should fall back to a string.")
private String format;

@JsonSetter("format")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.fasterxml.jackson.annotation.*;

/**
* A struct represents a class/structure with a fix set of defined properties.
*/
@JsonClassDescription("A struct represents a class/structure with a fix set of defined properties.")
public class StructDefinitionType extends DefinitionType {
@JsonPropertyDescription("Indicates whether this is a base structure, default is false. If true the structure is used a base type, this means it is not possible to create an instance from this structure.")
private Boolean base;
@JsonPropertyDescription("Optional the property name of a discriminator property. This should be only used in case this is also a base structure.")
private String discriminator;
@JsonPropertyDescription("In case a discriminator is configured it is required to configure a mapping. The mapping is a map where the key is the type name and the value the actual discriminator type value.")
private java.util.Map<String, String> mapping;
@JsonPropertyDescription("Defines a parent type for this structure. Some programming languages like Go do not support the concept of an extends, in this case the code generator simply copies all properties into this structure.")
private ReferencePropertyType parent;
@JsonPropertyDescription("Contains a map of available properties for this struct.")
private java.util.Map<String, PropertyType> properties;

@JsonSetter("base")
Expand Down
6 changes: 3 additions & 3 deletions tests/Generator/resource/java/complex/TypeSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.fasterxml.jackson.annotation.*;

/**
* TypeSchema specification
*/
@JsonClassDescription("TypeSchema specification")
public class TypeSchema {
private java.util.Map<String, DefinitionType> definitions;
@JsonPropertyDescription("Through the import keyword it is possible to import other TypeSchema documents. It contains a map where the key is the namespace and the value points to a remote document. The value is a URL and a code generator should support at least the following schemes: file, http, https.")
private java.util.Map<String, String> _import;
@JsonPropertyDescription("Specifies the root type of your specification.")
private String root;

@JsonSetter("definitions")
Expand Down
6 changes: 3 additions & 3 deletions tests/Generator/resource/java/default/Author.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

import com.fasterxml.jackson.annotation.*;

/**
* An simple author element with some description
*/
@JsonClassDescription("An simple author element with some description")
public class Author {
private String title;
@JsonPropertyDescription("We will send no spam to this address")
private String email;
private java.util.List<String> categories;
@JsonPropertyDescription("Array of locations")
private java.util.List<Location> locations;
private Location origin;

Expand Down
4 changes: 1 addition & 3 deletions tests/Generator/resource/java/default/Location.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* Location of the person
*/
@JsonClassDescription("Location of the person")
public class Location {
private Double lat;
private Double _long;
Expand Down
5 changes: 2 additions & 3 deletions tests/Generator/resource/java/default/News.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import com.fasterxml.jackson.annotation.*;

/**
* An general news entry
*/
@JsonClassDescription("An general news entry")
public class News {
private Meta config;
private java.util.Map<String, String> inlineConfig;
Expand All @@ -18,6 +16,7 @@ public class News {
private java.time.LocalDateTime readDate;
private Double price;
private Integer rating;
@JsonPropertyDescription("Contains the main content of the news entry")
private String content;
private String question;
private String version;
Expand Down

0 comments on commit 40d639e

Please sign in to comment.