Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #363 from Azure/feature/allow-binary-file-with-jso…
Browse files Browse the repository at this point in the history
…n-content-type

Allow binary files with json content type
  • Loading branch information
daviwil authored Dec 3, 2020
2 parents 6f46b2d + e26442f commit 8024c8d
Show file tree
Hide file tree
Showing 7 changed files with 637 additions and 2 deletions.
28 changes: 26 additions & 2 deletions modelerfour/modeler/modelerfour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ export class ModelerFour {
return this.processChoiceSchema(name, schema);
}

if (<any>schema.type === 'file' || <any>schema.format === 'file' || <any>schema.format === 'binary') {
if (this.isSchemaBinary(schema)) {
// handle inconsistency in file format handling.
this.session.hint(
`'The schema ${schema?.['x-ms-metadata']?.name || name} with 'type: ${schema.type}', format: ${schema.format}' will be treated as a binary blob for binary media types.`,
Expand Down Expand Up @@ -1760,7 +1760,9 @@ export class ModelerFour {
throw new Error(`Operation '${operationGroup.language.default.name}/${operation.language.default.name}' must have a media type.`);
}
}

const kmtBinary = groupedMediaTypes.get(KnownMediaType.Binary);

if (kmtBinary) {
// handle binary
this.processBinary(KnownMediaType.Binary, kmtBinary, operation, requestBody);
Expand All @@ -1771,7 +1773,21 @@ export class ModelerFour {
}
const kmtJSON = groupedMediaTypes.get(KnownMediaType.Json);
if (kmtJSON) {
this.processSerializedObject(KnownMediaType.Json, kmtJSON, operation, requestBody);
if ([...kmtJSON.values()].find((x) => x.schema.instance && this.isSchemaBinary(x.schema.instance))) {
this.processBinary(
KnownMediaType.Binary,
kmtJSON,
operation,
requestBody
);
} else {
this.processSerializedObject(
KnownMediaType.Json,
kmtJSON,
operation,
requestBody
);
}
}
const kmtXML = groupedMediaTypes.get(KnownMediaType.Xml);
if (kmtXML && !kmtJSON) {
Expand Down Expand Up @@ -1882,6 +1898,14 @@ export class ModelerFour {
return this.codeModel;
}

private isSchemaBinary(schema: OpenAPI.Schema) {
return (
<any>schema.type === "file" ||
<any>schema.format === "file" ||
<any>schema.format === "binary"
);
}

private propagateSchemaUsage(schema: Schema): void {
const processedSchemas = new Set<Schema>();

Expand Down
110 changes: 110 additions & 0 deletions modelerfour/test/scenarios/body-binary-json/flattened.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
!<!CodeModel>
info: !<!Info>
description: Acceptance test for file with json content type.
title: 'Binary with content-type: application/json'
schemas: !<!Schemas>
strings:
- !<!StringSchema> &ref_0
type: string
language: !<!Languages>
default:
name: string
description: simple string
protocol: !<!Protocols> {}
binaries:
- !<!BinarySchema> &ref_2
type: binary
apiVersions:
- !<!ApiVersion>
version: 1.0.0
language: !<!Languages>
default:
name: binary
description: ''
protocol: !<!Protocols> {}
globalParameters:
- !<!Parameter> &ref_1
schema: *ref_0
clientDefaultValue: 'https://localhost'
implementation: Client
origin: 'modelerfour:synthesized/host'
required: true
extensions:
x-ms-skip-url-encoding: true
language: !<!Languages>
default:
name: $host
description: server parameter
serializedName: $host
protocol: !<!Protocols>
http: !<!HttpParameter>
in: uri
operationGroups:
- !<!OperationGroup>
$key: Upload
operations:
- !<!Operation>
apiVersions:
- !<!ApiVersion>
version: 1.0.0
parameters:
- *ref_1
requests:
- !<!Request>
parameters:
- !<!Parameter> &ref_3
schema: *ref_2
implementation: Method
required: true
language: !<!Languages>
default:
name: fileParam
description: Foo bar
protocol: !<!Protocols>
http: !<!HttpParameter>
in: body
style: binary
signatureParameters:
- *ref_3
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpBinaryRequest>
path: /file
method: post
binary: true
knownMediaType: binary
mediaTypes:
- application/json
uri: '{$host}'
signatureParameters: []
responses:
- !<!Response>
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpResponse>
statusCodes:
- '200'
language: !<!Languages>
default:
name: File
description: Uploading json file
protocol: !<!Protocols> {}
language: !<!Languages>
default:
name: Upload
description: ''
protocol: !<!Protocols> {}
security: !<!Security>
authenticationRequired: false
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpModel> {}
110 changes: 110 additions & 0 deletions modelerfour/test/scenarios/body-binary-json/grouped.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
!<!CodeModel>
info: !<!Info>
description: Acceptance test for file with json content type.
title: 'Binary with content-type: application/json'
schemas: !<!Schemas>
strings:
- !<!StringSchema> &ref_0
type: string
language: !<!Languages>
default:
name: string
description: simple string
protocol: !<!Protocols> {}
binaries:
- !<!BinarySchema> &ref_2
type: binary
apiVersions:
- !<!ApiVersion>
version: 1.0.0
language: !<!Languages>
default:
name: binary
description: ''
protocol: !<!Protocols> {}
globalParameters:
- !<!Parameter> &ref_1
schema: *ref_0
clientDefaultValue: 'https://localhost'
implementation: Client
origin: 'modelerfour:synthesized/host'
required: true
extensions:
x-ms-skip-url-encoding: true
language: !<!Languages>
default:
name: $host
description: server parameter
serializedName: $host
protocol: !<!Protocols>
http: !<!HttpParameter>
in: uri
operationGroups:
- !<!OperationGroup>
$key: Upload
operations:
- !<!Operation>
apiVersions:
- !<!ApiVersion>
version: 1.0.0
parameters:
- *ref_1
requests:
- !<!Request>
parameters:
- !<!Parameter> &ref_3
schema: *ref_2
implementation: Method
required: true
language: !<!Languages>
default:
name: fileParam
description: Foo bar
protocol: !<!Protocols>
http: !<!HttpParameter>
in: body
style: binary
signatureParameters:
- *ref_3
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpBinaryRequest>
path: /file
method: post
binary: true
knownMediaType: binary
mediaTypes:
- application/json
uri: '{$host}'
signatureParameters: []
responses:
- !<!Response>
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpResponse>
statusCodes:
- '200'
language: !<!Languages>
default:
name: File
description: Uploading json file
protocol: !<!Protocols> {}
language: !<!Languages>
default:
name: Upload
description: ''
protocol: !<!Protocols> {}
security: !<!Security>
authenticationRequired: false
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpModel> {}
110 changes: 110 additions & 0 deletions modelerfour/test/scenarios/body-binary-json/modeler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
!<!CodeModel>
info: !<!Info>
description: Acceptance test for file with json content type.
title: 'Binary with content-type: application/json'
schemas: !<!Schemas>
strings:
- !<!StringSchema> &ref_0
type: string
language: !<!Languages>
default:
name: string
description: simple string
protocol: !<!Protocols> {}
binaries:
- !<!BinarySchema> &ref_1
type: binary
apiVersions:
- !<!ApiVersion>
version: 1.0.0
language: !<!Languages>
default:
name: binary
description: ''
protocol: !<!Protocols> {}
globalParameters:
- !<!Parameter> &ref_3
schema: *ref_0
clientDefaultValue: 'https://localhost'
implementation: Client
origin: 'modelerfour:synthesized/host'
required: true
extensions:
x-ms-skip-url-encoding: true
language: !<!Languages>
default:
name: $host
description: server parameter
serializedName: $host
protocol: !<!Protocols>
http: !<!HttpParameter>
in: uri
operationGroups:
- !<!OperationGroup>
$key: Upload
operations:
- !<!Operation>
apiVersions:
- !<!ApiVersion>
version: 1.0.0
parameters:
- *ref_3
requests:
- !<!Request>
parameters:
- !<!Parameter> &ref_2
schema: *ref_1
implementation: Method
required: true
language: !<!Languages>
default:
name: fileParam
description: Foo bar
protocol: !<!Protocols>
http: !<!HttpParameter>
in: body
style: binary
signatureParameters:
- *ref_2
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpBinaryRequest>
path: /file
method: post
binary: true
knownMediaType: binary
mediaTypes:
- application/json
uri: '{$host}'
signatureParameters: []
responses:
- !<!Response>
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpResponse>
statusCodes:
- '200'
language: !<!Languages>
default:
name: File
description: Uploading json file
protocol: !<!Protocols> {}
language: !<!Languages>
default:
name: Upload
description: ''
protocol: !<!Protocols> {}
security: !<!Security>
authenticationRequired: false
language: !<!Languages>
default:
name: ''
description: ''
protocol: !<!Protocols>
http: !<!HttpModel> {}
Loading

0 comments on commit 8024c8d

Please sign in to comment.