Skip to content

Commit

Permalink
Model referenced in oneOf in path is ignored
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
luisfpg committed Aug 14, 2019
1 parent 3160d93 commit ce33aaf
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 24 deletions.
10 changes: 8 additions & 2 deletions lib/gen-type.ts
Expand Up @@ -41,7 +41,7 @@ export abstract class GenType {
this.additionalDependencies = [...this._additionalDependencies];
}

protected collectImports(schema: SchemaObject | ReferenceObject | undefined, additional?: true): void {
protected collectImports(schema: SchemaObject | ReferenceObject | undefined, additional = false, processOneOf = false): void {
if (!schema) {
return;
} else if (schema.$ref) {
Expand All @@ -55,12 +55,18 @@ export abstract class GenType {
schema = schema as SchemaObject;
(schema.allOf || []).forEach(i => this.collectImports(i, additional));
(schema.anyOf || []).forEach(i => this.collectImports(i, additional));
if (processOneOf) {
(schema.oneOf || []).forEach(i => this.collectImports(i, additional));
}
if (schema.items) {
this.collectImports(schema.items, additional);
}
if (schema.properties) {
const properties = schema.properties;
Object.keys(properties).forEach(p => this.collectImports(properties[p], additional));
Object.keys(properties).forEach(p => {
const prop = properties[p];
this.collectImports(prop, additional, true);
});
}
if (typeof schema.additionalProperties === 'object') {
this.collectImports(schema.additionalProperties, additional);
Expand Down
6 changes: 3 additions & 3 deletions lib/service.ts
Expand Up @@ -25,7 +25,7 @@ export class Service extends GenType {
// Collect the imports
for (const operation of operations) {
for (const parameter of operation.parameters) {
this.collectImports(parameter.spec.schema);
this.collectImports(parameter.spec.schema, false, true);
}
for (const securityGroup of operation.security) {
securityGroup.forEach(security => this.collectImports(security.spec.schema));
Expand All @@ -36,9 +36,9 @@ export class Service extends GenType {
}
}
for (const response of operation.allResponses) {
const additional = response === operation.successResponse ? undefined : true;
const additional = response !== operation.successResponse;
for (const content of response.content) {
this.collectImports(content.spec.schema, additional);
this.collectImports(content.spec.schema, additional, true);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/requestBuilder.handlebars
Expand Up @@ -165,7 +165,7 @@ export class {{ requestBuilderClass }} {
const value = this._header.get(param);
if (value instanceof Array) {
for (const item of value) {
httpHeaders = httpHeaders.append(param, (item !== undefined && item !== null ? item || '').toString());
httpHeaders = httpHeaders.append(param, (item !== undefined && item !== null ? item : '').toString());
}
} else {
httpHeaders = httpHeaders.set(param, (value !== undefined && value !== null ? value : '').toString());
Expand Down
120 changes: 114 additions & 6 deletions test/all-types.json
Expand Up @@ -5,7 +5,7 @@
"version": "1.0"
},
"paths": {
"/test": {
"/foo": {
"get": {
"responses": {
"200": {
Expand All @@ -19,6 +19,66 @@
}
}
}
},
"/bar": {
"parameters": [
{
"name": "param",
"in": "query",
"required": false,
"schema": {
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/ReferencedInParamOneOf1"
},
{
"$ref": "#/components/schemas/ReferencedInParamOneOf2"
}
]
}
}
],
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/ReferencedInServiceOneOf1"
},
{
"$ref": "#/components/schemas/ReferencedInServiceOneOf2"
}
]
}
}
}
}
}
}
},
"/baz": {
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Disjunct"
}
]
}
}
}
}
}
}
}
},
"components": {
Expand Down Expand Up @@ -70,8 +130,13 @@
"Disjunct": {
"type": "object",
"properties": {
"id": {
"type": "string"
"ref": {
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/ReferencedInNullableOneOf"
}
]
}
},
"oneOf": [
Expand All @@ -94,6 +159,46 @@
}
}
},
"ReferencedInNullableOneOf": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"ReferencedInServiceOneOf1": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"ReferencedInServiceOneOf2": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"ReferencedInParamOneOf1": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"ReferencedInParamOneOf2": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"Containers": {
"type": "array",
"items": {
Expand Down Expand Up @@ -210,6 +315,12 @@
"description": "Property of type array of any type",
"items": {}
},
"dynamic": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/OtherObject"
}
},
"nestedObject": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -245,9 +356,6 @@
}
}
}
},
"additionalProperties": {
"$ref": "#/components/schemas/OtherObject"
}
}
]
Expand Down

0 comments on commit ce33aaf

Please sign in to comment.