Skip to content

Commit

Permalink
Support nullable and datetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Aug 11, 2023
1 parent 11c09c3 commit d09cd62
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</PropertyGroup>

<PropertyGroup>
<FacilityVersion>2.10.1</FacilityVersion>
<FacilityVersion>2.11.0</FacilityVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion example/ExampleApi.fsd
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ service ExampleApi
[http(from: header)]
prices: decimal[];

[http(from: body)]
[http(from: body, type: "text/sink")]
text: string;
}

Expand Down Expand Up @@ -285,6 +285,9 @@ service ExampleApi

/// The price of the widget.
price: decimal;

/// When the widget was created.
created: datetime;
}

/// A widget job.
Expand Down Expand Up @@ -357,6 +360,8 @@ service ExampleApi

[tag(name: widgets)]
namedWidgets: map<Widget>;

ternary: nullable<boolean>;
}

/// Identifies a widget field.
Expand Down Expand Up @@ -397,6 +402,12 @@ service ExampleApi
[obsolete(message: "This field was never used.")]
oldField: string;
}

/// An external data type.
extern data ExternalDto;

/// An external enum.
extern enum ExternalEnum;
}

# ExampleApi
Expand Down
2 changes: 1 addition & 1 deletion example/output/fsd/SwaggerPetstore.fsd
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ service SwaggerPetstore

quantity: int32;

shipDate: string;
shipDate: datetime;

/// Order Status
status: string;
Expand Down
1 change: 1 addition & 0 deletions example/output/fsd/swagger/SwaggerPetstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ definitions:
format: int32
shipDate:
type: string
format: date-time
status:
description: Order Status
type: string
Expand Down
9 changes: 9 additions & 0 deletions example/output/swagger/ExampleApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@
"description": "The price of the widget.",
"type": "number",
"format": "decimal"
},
"created": {
"description": "When the widget was created.",
"type": "string",
"format": "date-time"
}
},
"x-remarks": "Additional DTO remarks.\n\n## Heading\n\nOnly top-level headings need to match a member name."
Expand Down Expand Up @@ -791,6 +796,10 @@
"additionalProperties": {
"$ref": "#/definitions/Widget"
}
},
"ternary": {
"type": "boolean",
"x-nullable": true
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions example/output/swagger/ExampleApi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ definitions:
description: The price of the widget.
type: number
format: decimal
created:
description: When the widget was created.
type: string
format: date-time
x-remarks: |-
Additional DTO remarks.
Expand Down Expand Up @@ -553,6 +557,9 @@ definitions:
type: object
additionalProperties:
$ref: '#/definitions/Widget'
ternary:
type: boolean
x-nullable: true
GetInfoResponse:
type: object
properties:
Expand Down
5 changes: 5 additions & 0 deletions example/output/swagger/fsd/ExampleApi.fsd
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ service ExampleApi

/// The price of the widget.
price: decimal;

/// When the widget was created.
created: datetime;
}

/// A preference.
Expand Down Expand Up @@ -326,6 +329,8 @@ service ExampleApi
namedStrings: map<string>;

namedWidgets: map<Widget>;

ternary: boolean;
}

data KitchenSink
Expand Down
2 changes: 1 addition & 1 deletion src/Facility.Definition.Swagger/SwaggerConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private SwaggerResponse ResolveResponse(SwaggerResponse swaggerResponse, Service
switch (swaggerSchema.Type ?? SwaggerSchemaType.Object)
{
case SwaggerSchemaType.String:
return swaggerSchema.Format == SwaggerSchemaTypeFormat.Byte ? "bytes" : "string";
return swaggerSchema.Format == SwaggerSchemaTypeFormat.DateTime ? "datetime" : swaggerSchema.Format == SwaggerSchemaTypeFormat.Byte ? "bytes" : "string";

case SwaggerSchemaType.Number:
return swaggerSchema.Format == SwaggerSchemaTypeFormat.Decimal ? "decimal" : "double";
Expand Down
11 changes: 11 additions & 0 deletions src/Facility.Definition.Swagger/SwaggerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ private static T GetTypeSchema<T>(ServiceTypeInfo type)
return new T { Type = SwaggerSchemaType.Number, Format = SwaggerSchemaTypeFormat.Decimal };
case ServiceTypeKind.Bytes:
return new T { Type = SwaggerSchemaType.String, Format = SwaggerSchemaTypeFormat.Byte };
case ServiceTypeKind.DateTime:
return new T { Type = SwaggerSchemaType.String, Format = SwaggerSchemaTypeFormat.DateTime };
case ServiceTypeKind.Object:
return new T { Type = SwaggerSchemaType.Object };
case ServiceTypeKind.Error:
Expand All @@ -409,6 +411,8 @@ private static T GetTypeSchema<T>(ServiceTypeInfo type)
return GetArrayOfSchema<T>(type.ValueType!);
case ServiceTypeKind.Map:
return (T) (object) GetMapOfSchema(type.ValueType!);
case ServiceTypeKind.Nullable:
return (T) (object) GetNullableOfSchema(type.ValueType!);
default:
throw new InvalidOperationException("Unexpected field type kind: " + type.Kind);
}
Expand Down Expand Up @@ -500,6 +504,13 @@ private static SwaggerSchema GetMapOfSchema(ServiceTypeInfo type)
};
}

private static SwaggerSchema GetNullableOfSchema(ServiceTypeInfo type)
{
var schema = GetTypeSchema<SwaggerSchema>(type);
schema.Nullable = true;
return schema;
}

private static object? ConvertJTokenToObject(JToken token)
{
if (token is JValue value)
Expand Down
4 changes: 4 additions & 0 deletions src/Facility.Definition.Swagger/SwaggerSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public class SwaggerSchema : ISwaggerSchema
[YamlMember(Alias = "x-obsolete")]
public bool? Obsolete { get; set; } // parameters, headers, schema

[JsonProperty("x-nullable")]
[YamlMember(Alias = "x-nullable")]
public bool? Nullable { get; set; } // schema

[JsonProperty("x-remarks")]
[YamlMember(Alias = "x-remarks")]
public string? Remarks { get; set; } // schema
Expand Down

0 comments on commit d09cd62

Please sign in to comment.