Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed php namespaces in nullable properties
  • Loading branch information
Eric Durand-Tremblay committed Jun 14, 2022
commit d7c4fdc560f3ea7145000a9aa833f1d58b1bbc51
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -331,7 +332,22 @@ public String getTypeDeclaration(Schema p) {
String type = super.getTypeDeclaration(p);
return (!languageSpecificPrimitives.contains(type))
? "\\" + modelPackage + "\\" + type : type;
} else if (p instanceof ComposedSchema) {
// Support nullable defined using oneOf construct
ComposedSchema composedSchema = (ComposedSchema)p;
if (Boolean.TRUE.equals(p.getNullable()) || ModelUtils.isNullableComposedSchema(composedSchema)) {
Schema inner = composedSchema
.getOneOf()
.stream()
.filter(
subSchema -> !ModelUtils.isNullType(subSchema)
).findFirst().orElse(null);
if (inner != null) {
return this.getTypeDeclaration(inner);
}
}
}

return super.getTypeDeclaration(p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import java.util.Arrays;
import java.util.HashMap;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertFalse;

public class AbstractPhpCodegenTest {

@Test
Expand Down Expand Up @@ -166,6 +170,39 @@ public void testEnumPropertyWithDefaultValue() {
Assert.assertEquals(cp1.getDefaultValue(), "'VALUE'");
}

@Test(description = "Issue #10593")
public void testModelWithNullableObjectProperty() {
final AbstractPhpCodegen codegen = new P_AbstractPhpCodegen();

final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_10593.yaml");
codegen.setOpenAPI(openAPI);
Schema schema = openAPI.getComponents().getSchemas().get("ModelWithNullableObjectProperty");

String propertyName;
CodegenProperty property;

// regular property
propertyName = "propertyName";
property = codegen.fromProperty(propertyName, (Schema) schema.getProperties().get(propertyName));
assertEquals(property.openApiType, "PropertyType");
assertFalse(property.isNullable);
assertEquals(property.dataType, "\\php\\Model\\PropertyType");

// openapi 3.0 nullable
propertyName = "propertyName30";
property = codegen.fromProperty(propertyName, (Schema) schema.getProperties().get(propertyName));
assertEquals(property.openApiType, "PropertyType");
assertTrue(property.isNullable);
assertEquals(property.dataType, "\\php\\Model\\PropertyType");

// openapi 3.1 'null'
propertyName = "propertyName31";
property = codegen.fromProperty(propertyName, (Schema) schema.getProperties().get(propertyName));
assertEquals(property.openApiType, "PropertyType");
assertTrue(property.isNullable);
assertEquals(property.dataType, "\\php\\Model\\PropertyType");
}

private static class P_AbstractPhpCodegen extends AbstractPhpCodegen {
@Override
public CodegenType getTag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ components:
schemas:
ModelWithNullableObjectProperty:
properties:
propertyName:
$ref: '#/components/schemas/PropertyType'
propertyName30:
nullable: true
oneOf:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ components:
schemas:
ModelWithNullableObjectProperty:
example:
propertyName:
foo: foo
nonNullableProperty: ""
propertyName31: ""
propertyName30: ""
propertyWithNullAndTwoTypes: ""
properties:
propertyName:
$ref: '#/components/schemas/PropertyType'
propertyName30:
nullable: true
oneOf:
Expand All @@ -43,6 +47,8 @@ components:
- $ref: '#/components/schemas/PropertyType'
- $ref: '#/components/schemas/OtherPropertyType'
PropertyType:
example:
foo: foo
properties:
foo:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**propertyName** | [**PropertyType**](PropertyType.md) | | [optional]
**propertyName30** | [**PropertyType**](PropertyType.md) | | [optional]
**propertyName31** | [**PropertyType**](PropertyType.md) | | [optional]
**nonNullableProperty** | [**OneOfstringnumber**](OneOfstringnumber.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@
* ModelWithNullableObjectProperty
*/
@JsonPropertyOrder({
ModelWithNullableObjectProperty.JSON_PROPERTY_PROPERTY_NAME,
ModelWithNullableObjectProperty.JSON_PROPERTY_PROPERTY_NAME30,
ModelWithNullableObjectProperty.JSON_PROPERTY_PROPERTY_NAME31,
ModelWithNullableObjectProperty.JSON_PROPERTY_NON_NULLABLE_PROPERTY,
ModelWithNullableObjectProperty.JSON_PROPERTY_PROPERTY_WITH_NULL_AND_TWO_TYPES
})
@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ModelWithNullableObjectProperty {
public static final String JSON_PROPERTY_PROPERTY_NAME = "propertyName";
private PropertyType propertyName;

public static final String JSON_PROPERTY_PROPERTY_NAME30 = "propertyName30";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the result for java

private JsonNullable<PropertyType> propertyName30 = JsonNullable.<PropertyType>undefined();

Expand All @@ -58,6 +62,32 @@ public class ModelWithNullableObjectProperty {
private JsonNullable<OneOfnullPropertyTypeOtherPropertyType> propertyWithNullAndTwoTypes = JsonNullable.<OneOfnullPropertyTypeOtherPropertyType>undefined();


public ModelWithNullableObjectProperty propertyName(PropertyType propertyName) {
this.propertyName = propertyName;
return this;
}

/**
* Get propertyName
* @return propertyName
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PROPERTY_NAME)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)

public PropertyType getPropertyName() {
return propertyName;
}


@JsonProperty(JSON_PROPERTY_PROPERTY_NAME)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public void setPropertyName(PropertyType propertyName) {
this.propertyName = propertyName;
}


public ModelWithNullableObjectProperty propertyName30(PropertyType propertyName30) {
this.propertyName30 = JsonNullable.<PropertyType>of(propertyName30);
return this;
Expand Down Expand Up @@ -206,7 +236,8 @@ public boolean equals(Object o) {
return false;
}
ModelWithNullableObjectProperty modelWithNullableObjectProperty = (ModelWithNullableObjectProperty) o;
return equalsNullable(this.propertyName30, modelWithNullableObjectProperty.propertyName30) &&
return Objects.equals(this.propertyName, modelWithNullableObjectProperty.propertyName) &&
equalsNullable(this.propertyName30, modelWithNullableObjectProperty.propertyName30) &&
equalsNullable(this.propertyName31, modelWithNullableObjectProperty.propertyName31) &&
equalsNullable(this.nonNullableProperty, modelWithNullableObjectProperty.nonNullableProperty) &&
equalsNullable(this.propertyWithNullAndTwoTypes, modelWithNullableObjectProperty.propertyWithNullAndTwoTypes);
Expand All @@ -218,7 +249,7 @@ private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b)

@Override
public int hashCode() {
return Objects.hash(hashCodeNullable(propertyName30), hashCodeNullable(propertyName31), hashCodeNullable(nonNullableProperty), hashCodeNullable(propertyWithNullAndTwoTypes));
return Objects.hash(propertyName, hashCodeNullable(propertyName30), hashCodeNullable(propertyName31), hashCodeNullable(nonNullableProperty), hashCodeNullable(propertyWithNullAndTwoTypes));
}

private static <T> int hashCodeNullable(JsonNullable<T> a) {
Expand All @@ -232,6 +263,7 @@ private static <T> int hashCodeNullable(JsonNullable<T> a) {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ModelWithNullableObjectProperty {\n");
sb.append(" propertyName: ").append(toIndentedString(propertyName)).append("\n");
sb.append(" propertyName30: ").append(toIndentedString(propertyName30)).append("\n");
sb.append(" propertyName31: ").append(toIndentedString(propertyName31)).append("\n");
sb.append(" nonNullableProperty: ").append(toIndentedString(nonNullableProperty)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**propertyName** | [**PropertyType**](PropertyType.md) | | [optional]
**propertyName30** | [**PropertyType**](PropertyType.md) | | [optional]
**propertyName31** | [**PropertyType**](PropertyType.md) | | [optional]
**nonNullableProperty** | [**OneOfLessThanStringCommaNumberGreaterThan**](OneOfLessThanStringCommaNumberGreaterThan.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.io.Serializable
/**
*
*
* @param propertyName
* @param propertyName30
* @param propertyName31
* @param nonNullableProperty
Expand All @@ -38,6 +39,9 @@ import java.io.Serializable

data class ModelWithNullableObjectProperty (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what we tried to achieve : val propertyName31: PropertyType? = null,


@Json(name = "propertyName")
val propertyName: PropertyType? = null,

@Json(name = "propertyName30")
val propertyName30: PropertyType? = null,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.gitignore
.openapi-generator-ignore
.php_cs
.travis.yml
README.md
Expand All @@ -19,7 +18,3 @@ lib/Model/OtherPropertyType.php
lib/Model/PropertyType.php
lib/ObjectSerializer.php
phpunit.xml.dist
test/Api/DefaultApiTest.php
test/Model/ModelWithNullableObjectPropertyTest.php
test/Model/OtherPropertyTypeTest.php
test/Model/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**property_name30** | [**PropertyType**](PropertyType.md) | | [optional]
**property_name31** | [**PropertyType**](PropertyType.md) | | [optional]
**property_name** | [**\OpenAPI\Client\Model\PropertyType**](PropertyType.md) | | [optional]
**property_name30** | [**\OpenAPI\Client\Model\PropertyType**](PropertyType.md) | | [optional]
**property_name31** | [**\OpenAPI\Client\Model\PropertyType**](PropertyType.md) | | [optional]
**non_nullable_property** | [**OneOfStringNumber**](OneOfStringNumber.md) | | [optional]
**property_with_null_and_two_types** | [**OneOfNullPropertyTypeOtherPropertyType**](OneOfNullPropertyTypeOtherPropertyType.md) | | [optional]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class ModelWithNullableObjectProperty implements ModelInterface, ArrayAccess, \J
* @var string[]
*/
protected static $openAPITypes = [
'property_name30' => 'PropertyType',
'property_name31' => 'PropertyType',
'property_name' => '\OpenAPI\Client\Model\PropertyType',
'property_name30' => '\OpenAPI\Client\Model\PropertyType',
'property_name31' => '\OpenAPI\Client\Model\PropertyType',
'non_nullable_property' => 'OneOfStringNumber',
'property_with_null_and_two_types' => 'OneOfNullPropertyTypeOtherPropertyType'
];
Expand All @@ -73,6 +74,7 @@ class ModelWithNullableObjectProperty implements ModelInterface, ArrayAccess, \J
* @psalm-var array<string, string|null>
*/
protected static $openAPIFormats = [
'property_name' => null,
'property_name30' => null,
'property_name31' => null,
'non_nullable_property' => null,
Expand Down Expand Up @@ -106,6 +108,7 @@ public static function openAPIFormats()
* @var string[]
*/
protected static $attributeMap = [
'property_name' => 'propertyName',
'property_name30' => 'propertyName30',
'property_name31' => 'propertyName31',
'non_nullable_property' => 'nonNullableProperty',
Expand All @@ -118,6 +121,7 @@ public static function openAPIFormats()
* @var string[]
*/
protected static $setters = [
'property_name' => 'setPropertyName',
'property_name30' => 'setPropertyName30',
'property_name31' => 'setPropertyName31',
'non_nullable_property' => 'setNonNullableProperty',
Expand All @@ -130,6 +134,7 @@ public static function openAPIFormats()
* @var string[]
*/
protected static $getters = [
'property_name' => 'getPropertyName',
'property_name30' => 'getPropertyName30',
'property_name31' => 'getPropertyName31',
'non_nullable_property' => 'getNonNullableProperty',
Expand Down Expand Up @@ -193,6 +198,7 @@ public function getModelName()
*/
public function __construct(array $data = null)
{
$this->container['property_name'] = $data['property_name'] ?? null;
$this->container['property_name30'] = $data['property_name30'] ?? null;
$this->container['property_name31'] = $data['property_name31'] ?? null;
$this->container['non_nullable_property'] = $data['non_nullable_property'] ?? null;
Expand Down Expand Up @@ -223,10 +229,34 @@ public function valid()
}


/**
* Gets property_name
*
* @return \OpenAPI\Client\Model\PropertyType|null
*/
public function getPropertyName()
{
return $this->container['property_name'];
}

/**
* Sets property_name
*
* @param \OpenAPI\Client\Model\PropertyType|null $property_name property_name
*
* @return self
*/
public function setPropertyName($property_name)
{
$this->container['property_name'] = $property_name;

return $this;
}

/**
* Gets property_name30
*
* @return PropertyType|null
* @return \OpenAPI\Client\Model\PropertyType|null
*/
public function getPropertyName30()
{
Expand All @@ -236,7 +266,7 @@ public function getPropertyName30()
/**
* Sets property_name30
*
* @param PropertyType|null $property_name30 property_name30
* @param \OpenAPI\Client\Model\PropertyType|null $property_name30 property_name30
*
* @return self
*/
Expand All @@ -250,7 +280,7 @@ public function setPropertyName30($property_name30)
/**
* Gets property_name31
*
* @return PropertyType|null
* @return \OpenAPI\Client\Model\PropertyType|null
*/
public function getPropertyName31()
{
Expand All @@ -260,7 +290,7 @@ public function getPropertyName31()
/**
* Sets property_name31
*
* @param PropertyType|null $property_name31 property_name31
* @param \OpenAPI\Client\Model\PropertyType|null $property_name31 property_name31
*
* @return self
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import {
* @interface ModelWithNullableObjectProperty
*/
export interface ModelWithNullableObjectProperty {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the fix for typescript

/**
*
* @type {PropertyType}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@type annotation is no longer correct (should be PropertyType | null).

honestly, I don't know why it's needed anyway, in the presence of the real type definitions.
but if it's there and not in sync with the actual type, I'm concerned it might confuse some tools/IDEs.
Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Humm, I did not change the templates, but if you want my personal advice, I would remove the @type. Not in this pr of course.

* @memberof ModelWithNullableObjectProperty
*/
propertyName?: PropertyType;
/**
*
* @type {PropertyType}
Expand Down Expand Up @@ -66,6 +72,7 @@ export function ModelWithNullableObjectPropertyFromJSONTyped(json: any, ignoreDi
}
return {

'propertyName': !exists(json, 'propertyName') ? undefined : PropertyTypeFromJSON(json['propertyName']),
'propertyName30': !exists(json, 'propertyName30') ? undefined : PropertyTypeFromJSON(json['propertyName30']),
'propertyName31': !exists(json, 'propertyName31') ? undefined : PropertyTypeFromJSON(json['propertyName31']),
'nonNullableProperty': !exists(json, 'nonNullableProperty') ? undefined : string | numberFromJSON(json['nonNullableProperty']),
Expand All @@ -82,6 +89,7 @@ export function ModelWithNullableObjectPropertyToJSON(value?: ModelWithNullableO
}
return {

'propertyName': PropertyTypeToJSON(value.propertyName),
'propertyName30': PropertyTypeToJSON(value.propertyName30),
'propertyName31': PropertyTypeToJSON(value.propertyName31),
'nonNullableProperty': string | numberToJSON(value.nonNullableProperty),
Expand Down