* new sample: openapi not swagger

* new check: is swagger 2.0

* additional properties polyfill test

* fixed clear-output-folder

* cleanup
This commit is contained in:
Johannes Bader 2017-09-06 15:52:31 -07:00 коммит произвёл GitHub
Родитель ab4e371273
Коммит 9a76766378
21 изменённых файлов: 525 добавлений и 18 удалений

Просмотреть файл

@ -29,6 +29,13 @@ directive:
url.split("/Microsoft.Storage/")[1].split("/")[0]; $.name.raw = res;
$.summary = JSON.stringify($, null, 2);
reason: We wanna group methods by URI.
- from: swagger-document
where: $.definitions.*.additionalProperties
transform: |
return typeof $ === "boolean"
? ($ ? { type: "object" } : undefined)
: $
reason: polyfill
input-file:
- >-
https://github.com/Azure/azure-rest-api-specs/blob/d374d03801e97737ddb32e01f20513e7b2bbd9c3/arm-storage/2015-06-15/swagger/storage.json

Просмотреть файл

@ -0,0 +1,146 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool
{
using Microsoft.Rest;
using Microsoft.Rest.Serialization;
using Models;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
/// <summary>
/// This is a sample.
/// </summary>
public partial class CowbellUniversity : ServiceClient<CowbellUniversity>, ICowbellUniversity
{
/// <summary>
/// The base URI of the service.
/// </summary>
public System.Uri BaseUri { get; set; }
/// <summary>
/// Gets or sets json serialization settings.
/// </summary>
public JsonSerializerSettings SerializationSettings { get; private set; }
/// <summary>
/// Gets or sets json deserialization settings.
/// </summary>
public JsonSerializerSettings DeserializationSettings { get; private set; }
/// <summary>
/// Initializes a new instance of the CowbellUniversity class.
/// </summary>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
public CowbellUniversity(params DelegatingHandler[] handlers) : base(handlers)
{
Initialize();
}
/// <summary>
/// Initializes a new instance of the CowbellUniversity class.
/// </summary>
/// <param name='rootHandler'>
/// Optional. The http client handler used to handle http transport.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
public CowbellUniversity(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandler, handlers)
{
Initialize();
}
/// <summary>
/// Initializes a new instance of the CowbellUniversity class.
/// </summary>
/// <param name='baseUri'>
/// Optional. The base URI of the service.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
/// <exception cref="System.ArgumentNullException">
/// Thrown when a required parameter is null
/// </exception>
public CowbellUniversity(System.Uri baseUri, params DelegatingHandler[] handlers) : this(handlers)
{
if (baseUri == null)
{
throw new System.ArgumentNullException("baseUri");
}
BaseUri = baseUri;
}
/// <summary>
/// Initializes a new instance of the CowbellUniversity class.
/// </summary>
/// <param name='baseUri'>
/// Optional. The base URI of the service.
/// </param>
/// <param name='rootHandler'>
/// Optional. The http client handler used to handle http transport.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
/// <exception cref="System.ArgumentNullException">
/// Thrown when a required parameter is null
/// </exception>
public CowbellUniversity(System.Uri baseUri, HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : this(rootHandler, handlers)
{
if (baseUri == null)
{
throw new System.ArgumentNullException("baseUri");
}
BaseUri = baseUri;
}
/// <summary>
/// An optional partial-method to perform custom initialization.
///</summary>
partial void CustomInitialize();
/// <summary>
/// Initializes client properties.
/// </summary>
private void Initialize()
{
BaseUri = new System.Uri("https://localhost");
SerializationSettings = new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter>
{
new Iso8601TimeSpanConverter()
}
};
DeserializationSettings = new JsonSerializerSettings
{
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter>
{
new Iso8601TimeSpanConverter()
}
};
CustomInitialize();
}
}
}

Просмотреть файл

@ -0,0 +1,34 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool
{
using Models;
using Newtonsoft.Json;
/// <summary>
/// This is a sample.
/// </summary>
public partial interface ICowbellUniversity : System.IDisposable
{
/// <summary>
/// The base URI of the service.
/// </summary>
System.Uri BaseUri { get; set; }
/// <summary>
/// Gets or sets json serialization settings.
/// </summary>
JsonSerializerSettings SerializationSettings { get; }
/// <summary>
/// Gets or sets json deserialization settings.
/// </summary>
JsonSerializerSettings DeserializationSettings { get; }
}
}

Просмотреть файл

@ -0,0 +1,42 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool.Models
{
using Newtonsoft.Json;
using System.Linq;
public partial class Cowbell1a
{
/// <summary>
/// Initializes a new instance of the Cowbell1a class.
/// </summary>
public Cowbell1a()
{
CustomInit();
}
/// <summary>
/// Initializes a new instance of the Cowbell1a class.
/// </summary>
public Cowbell1a(string name = default(string))
{
Name = name;
CustomInit();
}
/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
/// <summary>
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,42 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool.Models
{
using Newtonsoft.Json;
using System.Linq;
public partial class Cowbell1b
{
/// <summary>
/// Initializes a new instance of the Cowbell1b class.
/// </summary>
public Cowbell1b()
{
CustomInit();
}
/// <summary>
/// Initializes a new instance of the Cowbell1b class.
/// </summary>
public Cowbell1b(string name = default(string))
{
Name = name;
CustomInit();
}
/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
/// <summary>
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,54 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool.Models
{
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public partial class Cowbell2a
{
/// <summary>
/// Initializes a new instance of the Cowbell2a class.
/// </summary>
public Cowbell2a()
{
CustomInit();
}
/// <summary>
/// Initializes a new instance of the Cowbell2a class.
/// </summary>
/// <param name="additionalProperties">Unmatched properties from the
/// message are deserialized this collection</param>
public Cowbell2a(IDictionary<string, object> additionalProperties = default(IDictionary<string, object>), string name = default(string))
{
AdditionalProperties = additionalProperties;
Name = name;
CustomInit();
}
/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
/// <summary>
/// Gets or sets unmatched properties from the message are deserialized
/// this collection
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> AdditionalProperties { get; set; }
/// <summary>
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,54 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace AddPropsBool.Models
{
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public partial class Cowbell2b
{
/// <summary>
/// Initializes a new instance of the Cowbell2b class.
/// </summary>
public Cowbell2b()
{
CustomInit();
}
/// <summary>
/// Initializes a new instance of the Cowbell2b class.
/// </summary>
/// <param name="additionalProperties">Unmatched properties from the
/// message are deserialized this collection</param>
public Cowbell2b(IDictionary<string, object> additionalProperties = default(IDictionary<string, object>), string name = default(string))
{
AdditionalProperties = additionalProperties;
Name = name;
CustomInit();
}
/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
/// <summary>
/// Gets or sets unmatched properties from the message are deserialized
/// this collection
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> AdditionalProperties { get; set; }
/// <summary>
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,37 @@
swagger: '2.0'
info:
description: This is a sample.
version: 1.0.0
title: Cowbell University
host: localhost
schemes:
- https
consumes:
- application/json
produces:
- application/json
definitions:
Cowbell1a:
type: object
properties:
name:
type: string
Cowbell1b:
type: object
properties:
name:
type: string
additionalProperties: false
Cowbell2a:
type: object
properties:
name:
type: string
additionalProperties:
type: object
Cowbell2b:
type: object
properties:
name:
type: string
additionalProperties: true

Просмотреть файл

@ -0,0 +1,9 @@
# Test for polyfill: Additional properties `true/false`
> see https://aka.ms/autorest
``` yaml
input-file: add-props-bool.yaml
csharp:
- output-folder: Client
```

Просмотреть файл

@ -0,0 +1 @@
0

Просмотреть файл

@ -0,0 +1 @@

Просмотреть файл

@ -0,0 +1,3 @@
(C) 2017 **Microsoft Corporation.**
# AutoRest code generation utility.
https://aka.ms/autorest

Просмотреть файл

@ -0,0 +1,7 @@
# Test: Syntactically invalid OpenAPI definition in JSON
> see https://aka.ms/autorest
``` yaml
input-file: tiny.json
```

Просмотреть файл

@ -0,0 +1 @@
3

Просмотреть файл

@ -0,0 +1,4 @@
at ...
Error: File '/Samples/test/error-behavior/openapi-not-swagger/tiny.json' is not a valid OpenAPI 2.0 definition (expected 'swagger: 2.0')
FATAL: Error: File '/Samples/test/error-behavior/openapi-not-swagger/tiny.json' is not a valid OpenAPI 2.0 definition (expected 'swagger: 2.0')
FATAL: swagger-document/loader - FAILED

Просмотреть файл

@ -0,0 +1,4 @@
(C) 2017 **Microsoft Corporation.**
# AutoRest code generation utility.
https://aka.ms/autorest
Process() Cancelled due to exception : File '/Samples/test/error-behavior/openapi-not-swagger/tiny.json' is not a valid OpenAPI 2.0 definition (expected 'swagger: 2.0')

Просмотреть файл

@ -0,0 +1,55 @@
{
"info": {
"description": "This is a sample.",
"version": "1.0.0",
"title": "Cowbell Factory"
},
"host": "localhost",
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/cowbell": {
"post": {
"description": "A good description.",
"operationId": "Cowbell_Add",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/Cowbell"
}
}
],
"responses": {
"200": {
"description": "Cowbell was added."
}
}
}
}
},
"definitions": {
"Cowbell": {
"required": "name"
},
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"description": "A cowbell."
}
}

Просмотреть файл

@ -19,8 +19,7 @@ import { ShallowCopy } from "./lib/source-map/merging";
import { Message, Channel } from "./lib/message";
import { resolve as currentDirectory } from "path";
import { ChildProcess } from "child_process";
import { rmdir } from "@microsoft.azure/async-io"
import { CreateFolderUri, MakeRelativeUri, ReadUri, ResolveUri, WriteString } from "./lib/ref/uri";
import { CreateFolderUri, MakeRelativeUri, ReadUri, ResolveUri, WriteString, ClearFolder } from "./lib/ref/uri";
import { isLegacy, CreateConfiguration } from "./legacyCli";
import { DataStore } from "./lib/data-store/data-store";
import { EnhancedFileSystem, RealFileSystem } from './lib/file-system';
@ -84,7 +83,7 @@ ${Stringify(config).replace(/^---\n/, "")}
const view = await api.view;
let outstanding: Promise<void> = Promise.resolve();
api.GeneratedFile.Subscribe((_, file) => outstanding = outstanding.then(() => WriteString(file.uri, file.content)));
api.ClearFolder.Subscribe((_, folder) => outstanding = outstanding.then(async () => { try { await rmdir(folder); } catch (e) { } }));
api.ClearFolder.Subscribe((_, folder) => outstanding = outstanding.then(async () => { try { await ClearFolder(folder); } catch (e) { } }));
subscribeMessages(api, () => { });
// warn about `--` arguments
@ -263,7 +262,7 @@ async function currentMain(autorestArgs: string[]): Promise<number> {
// perform file system operations.
for (const folder of clearFolders) {
try { await rmdir(folder); } catch (e) { }
try { await ClearFolder(folder); } catch (e) { }
}
for (const artifact of artifacts) {
await WriteString(artifact.uri, artifact.content);

Просмотреть файл

@ -273,6 +273,10 @@ export async function LoadLiterateSwaggerOverride(config: ConfigurationView, inp
export async function LoadLiterateSwagger(config: ConfigurationView, inputScope: DataSource, inputFileUri: string, sink: DataSink): Promise<DataHandle> {
const data = await ParseLiterateYaml(config, await inputScope.ReadStrict(inputFileUri), sink);
// check OpenAPI version
if (data.ReadObject<any>().swagger !== "2.0") {
throw new Error(`File '${inputFileUri}' is not a valid OpenAPI 2.0 definition (expected 'swagger: 2.0')`);
}
const externalFiles: { [uri: string]: DataHandle } = {};
externalFiles[inputFileUri] = data;
await EnsureCompleteDefinitionIsPresent(config,

Просмотреть файл

@ -270,20 +270,8 @@ export function WriteString(fileUri: string, data: string): Promise<void> {
* Clears a folder on the local file system.
* @param folderUri Folder uri.
*/
import { rmdir } from "@microsoft.azure/async-io";
export async function ClearFolder(folderUri: string): Promise<void> {
const path = FileUriToLocalPath(folderUri);
const deleteFolderRecursive = async (path: string) => {
if (await exists(path)) {
for (const file of await readdir(path)) {
var curPath = path + "/" + file;
if (lstatSync(curPath).isDirectory()) {
await deleteFolderRecursive(curPath);
} else {
unlinkSync(curPath);
}
}
rmdirSync(path);
}
};
await deleteFolderRecursive(path);
return rmdir(path);
}

Просмотреть файл

@ -153,6 +153,21 @@ scope-cm/emitter:
"code-model-v1"
```
#### Polyfills
Support for `additionalProperties: true/false` in `definitions` section
``` yaml
directive:
- from: swagger-document
where: $.definitions.*.additionalProperties
transform: |
return typeof $ === "boolean"
? ($ ? { type: "object" } : undefined)
: $
reason: polyfill
```
#### Validation
``` yaml