Skip to content

Commit

Permalink
Support properties with type number (#27)
Browse files Browse the repository at this point in the history
* Removed `model.mustache`

* Add support for type `number`

* Add descriptions to Example Record with `number` fields
  • Loading branch information
Chrimle authored Sep 8, 2024
1 parent 3741892 commit dcc8fb8
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 74 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,25 @@ properties may be ignored, or may cause problems.
> The following properties are referring to the schema definitions, prefixed
> by `components.schemas.{schema}`.
| Property | Description | Value | Value Description |
|----------------------------------------------|--------------------------------------------------|:-----------------:|--------------------------------------------------------------------------------------------------------------------------|
| `{schema}` | Name of the generated Java class. | * | |
| `{schema}.type` | Type of the generated Java class. | `object` | Generates a Record class. |
| | | `enum` | Generates an Enum class. |
| `{schema}.description` | JavaDoc description of the generated Java class. | * | |
| `{schema}.deprecated` | Marks the generated Java class as Deprecated. | `true` | Annotates the class with `@Deprecated` and adds `@deprecated` to the JavaDoc description. |
| | | `false` (default) | Does nothing. |
| `{schema}.properties` | Fields of the generated Record class. | * | |
| `{schema}.properties.{property}` | Name of the field. | * | Added as a `@param` in the JavaDoc. |
| `{schema}.properties.{property}.description` | Description of the field. | * | Description of the `@param` in the JavaDoc. |
| `{schema}.properties.{property}.$ref` | Type of the field is another Java class. | * | |
| `{schema}.properties.{property}.type` | Type of the field. | `array` | Generates the field as `List<{items.type}>`. |
| | | `boolean` | Generates the field as `Boolean`. |
| | | `enum` | **Not supported yet** - See [Issue #5](https://github.com/Chrimle/openapi-to-java-records-mustache-templates/issues/5) |
| | | `integer` | Generates the field as `Integer`. |
| | | `number` | **Not supported yet** - See [Issue #15](https://github.com/Chrimle/openapi-to-java-records-mustache-templates/issues/15) |
| | | `object` | **Not supported yet** - See [Issue #4](https://github.com/Chrimle/openapi-to-java-records-mustache-templates/issues/4) |
| | | `string` | Generates the field as `String`. |
| Property | Description | Value | Value Description |
|----------------------------------------------|--------------------------------------------------|:-----------------:|------------------------------------------------------------------------------------------------------------------------|
| `{schema}` | Name of the generated Java class. | * | |
| `{schema}.type` | Type of the generated Java class. | `object` | Generates a Record class. |
| | | `enum` | Generates an Enum class. |
| `{schema}.description` | JavaDoc description of the generated Java class. | * | |
| `{schema}.deprecated` | Marks the generated Java class as Deprecated. | `true` | Annotates the class with `@Deprecated` and adds `@deprecated` to the JavaDoc description. |
| | | `false` (default) | Does nothing. |
| `{schema}.properties` | Fields of the generated Record class. | * | |
| `{schema}.properties.{property}` | Name of the field. | * | Added as a `@param` in the JavaDoc. |
| `{schema}.properties.{property}.description` | Description of the field. | * | Description of the `@param` in the JavaDoc. |
| `{schema}.properties.{property}.$ref` | Type of the field is another Java class. | * | |
| `{schema}.properties.{property}.type` | Type of the field. | `array` | Generates the field as `List<{items.type}>`. |
| | | `boolean` | Generates the field as `Boolean`. |
| | | `enum` | **Not supported yet** - See [Issue #5](https://github.com/Chrimle/openapi-to-java-records-mustache-templates/issues/5) |
| | | `integer` | Generates the field as `Integer`. |
| | | `number` | Generates the field as `BigDecimal`. |
| | | `object` | **Not supported yet** - See [Issue #4](https://github.com/Chrimle/openapi-to-java-records-mustache-templates/issues/4) |
| | | `string` | Generates the field as `String`. |

### Useful Resources

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<junit-jupiter-api.version>5.10.3</junit-jupiter-api.version>
<gson.version>2.10.1</gson.version>
</properties>


Expand All @@ -31,6 +32,11 @@
<version>${junit-jupiter-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>


Expand Down
24 changes: 13 additions & 11 deletions src/main/resources/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@ components:
description: yet another Array field
items:
type: boolean
###### NOT SUPPORTED YET ######
#ExampleRecordWithNumberFields:
# type: object
# properties:
# field1:
# type: number
# field2:
# type: number
# field3:
# type: number
###############################
ExampleRecordWithNumberFields:
type: object
description: Example of a Record with Number fields
properties:
field1:
type: number
description: a Number field
field2:
type: number
description: another Number field
field3:
type: number
description: yet another Number field
ExampleRecordWithExampleRecordFields:
type: object
description: Example of a Record with Record fields
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/templates/deprecation.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#isDeprecated}}

@Deprecated{{/isDeprecated}}
32 changes: 0 additions & 32 deletions src/main/resources/templates/model.mustache

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{>javadoc}}{{>deprecation}}
public enum {{classname}} {
{{#allowableValues}}{{#enumVars}}{{{name}}}{{^-last}},
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/templates/pojo.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{>javadoc}}{{#isDeprecated}}
@Deprecated{{/isDeprecated}}
{{^isEnum}}{{>record}}{{/isEnum}}{{#isEnum}}{{>enum}}{{/isEnum}}
{{>javadoc}}{{>deprecation}}
public record {{classname}}(
{{#vars}}{{{datatypeWithEnum}}} {{name}}{{^-last}},
{{/-last}}{{/vars}}) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.chrimle.example;

import com.chrimle.example.utils.AssertionUtils;
import java.math.BigDecimal;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("Testing Record with Number fields")
public class TestExampleRecordWithNumberFields {

private final Class<?> classUnderTest = ExampleRecordWithNumberFields.class;

@Test
@DisplayName("Testing class generated is a Record")
public void testClassGeneratedIsRecord() {
AssertionUtils.assertIsRecord(classUnderTest);
}

@Test
@DisplayName("Testing generated Record is not annotated as @Deprecated")
public void testRecordIsNotAnnotatedAsDeprecated() {
AssertionUtils.assertClassIsNotAnnotatedAsDeprecated(classUnderTest);
}

@Test
@DisplayName("Testing generated Record has expected number of fields of expected types")
public void testRecordHasNumberOfFieldsOfType() {
AssertionUtils.assertRecordHasNumberOfFieldsOfType(
classUnderTest, 3, BigDecimal.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.annotations.SerializedName;

/**
* Example of a deprecated Enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a deprecated Record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.annotations.SerializedName;

/**
* Example of an Enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a Record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a Record with Boolean fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.chrimle.example.ExampleEnum;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a Record with Enum fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.chrimle.example.ExampleRecord;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a Record with Record fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.List;
import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Arrays;

/**
* Example of a Record with Integer fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Example OpenAPI Spec.
* An example OpenAPI-spec to generate example Java records.
*
* The version of the OpenAPI document: 0.0.1
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
* This class was generated using custom mustache templates from
* openapi-to-java-records-mustache-templates. For further information,
* questions, requesting features or reporting issues, please visit:
* https://github.com/Chrimle/openapi-to-java-records-mustache-templates.
*
*/

package com.chrimle.example;

import java.util.Objects;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

/**
* Example of a Record with Number fields
* @param field1 a Number field
* @param field2 another Number field
* @param field3 yet another Number field
*/
public record ExampleRecordWithNumberFields(
BigDecimal field1,
BigDecimal field2,
BigDecimal field3) {

}
Loading

0 comments on commit dcc8fb8

Please sign in to comment.