Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update blueprint request body schema should not specified 'required' #71

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ Note that:
- `addSelectQueryParam`
- `addOmitQueryParam`
- `addModelBodyParam`
- `addModelBodyParamUpdate`
- `addResultOfArrayOfModels`
- `addAssociationPathParam`
- `addAssociationFKPathParam`
Expand Down
55 changes: 48 additions & 7 deletions lib/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,29 +293,44 @@ export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyM
return schemas;
}

const schema: OpenApi.UpdatedSchema = {
const schemaWithoutRequired: OpenApi.UpdatedSchema = {
description: model.swagger.modelSchema?.description || `Sails ORM Model **${model.globalId}**`,
properties: {},
required: [],
...omit(model.swagger?.modelSchema || {}, 'exclude', 'description', 'tags'),
...omit(model.swagger?.modelSchema || {}, 'exclude', 'description', 'required', 'tags'),
}

let required: string[] = [];

const attributes = model.attributes || {}
defaults(
schema.properties,
schemaWithoutRequired.properties,
Object.keys(attributes).reduce((props, attributeName) => {
const attribute = model.attributes[attributeName];
if (attribute.meta?.swagger?.exclude !== true) {
props[attributeName] = generateAttributeSchema(attribute);
if (attribute.required) schema.required!.push(attributeName);
if (attribute.required) required!.push(attributeName);
}
return props
}, {} as NameKeyMap<OpenApi.UpdatedSchema>)
);

if (schema.required!.length <= 0) delete schema.required;
const withoutRequiredName = `${model.identity}-without-required-constraint`;
const schema: OpenApi.UpdatedSchema = {
allOf: [
{ '$ref': `#/components/schemas/${withoutRequiredName}` },
],
};

if(model.swagger?.modelSchema?.required) {
required = [ ...model.swagger.modelSchema.required ];
}

if(required.length > 0) {
schema.allOf!.push({ required: required });
}

schemas[model.identity] = schema;
schemas[withoutRequiredName] = schemaWithoutRequired;

return schemas
}, {} as NameKeyMap<OpenApi.UpdatedSchema | Reference>)
Expand Down Expand Up @@ -478,13 +493,39 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
required: true,
content: {
'application/json': {
schema: { "$ref": "#/components/schemas/" + route.model!.identity }
schema: { '$ref': `#/components/schemas/${route.model!.identity}` }
},
},
};
}
},

addModelBodyParamUpdate: () => {
if (route.actionType === 'shortcutBlueprint') {
const schema = specification!.components!.schemas?.[route.model!.identity+'-without-required-constraint'];
if (schema) {
const resolvedSchema = resolveRef(specification, schema);
if (resolvedSchema) {
generateSchemaAsQueryParameters(resolvedSchema as OpenApi.UpdatedSchema).map(p => {
if (isParam('query', p.name)) return;
pathEntry.parameters.push(p);
});
}
}
} else {
if (pathEntry.requestBody) return;
pathEntry.requestBody = {
description: subst('JSON dictionary representing the {globalId} instance to update.'),
required: true,
content: {
'application/json': {
schema: { '$ref': `#/components/schemas/${route.model!.identity}-without-required-constraint` }
},
},
}
}
},

addResultOfArrayOfModels: () => {
assign(pathEntry.responses['200'], {
description: subst(template.resultDescription),
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum Modifiers {
ADD_SELECT_QUERY_PARAM = 'addSelectQueryParam',
ADD_OMIT_QUERY_PARAM = 'addOmitQueryParam',
ADD_MODEL_BODY_PARAM = 'addModelBodyParam',
ADD_MODEL_BODY_PARAM_UPDATE = 'addModelBodyParamUpdate',
ADD_RESULT_OF_ARRAY_OF_MODELS = 'addResultOfArrayOfModels',
ADD_ASSOCIATION_PATH_PARAM = 'addAssociationPathParam',
ADD_ASSOCIATION_FK_PATH_PARAM = 'addAssociationFKPathParam',
Expand Down
2 changes: 1 addition & 1 deletion lib/type-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const blueprintActionTemplates: BlueprintActionTemplates = {
],
resultDescription: 'Responds with the newly updated **{globalId}** record as a JSON dictionary',
notFoundDescription: 'Cannot update, **{globalId}** record with specified ID **NOT** found',
modifiers: [Modifiers.ADD_MODEL_BODY_PARAM, Modifiers.ADD_RESULT_OF_MODEL, Modifiers.ADD_RESULT_VALIDATION_ERROR, Modifiers.ADD_RESULT_NOT_FOUND, Modifiers.ADD_SHORTCUT_BLUEPRINT_ROUTE_NOTE]
modifiers: [Modifiers.ADD_MODEL_BODY_PARAM_UPDATE, Modifiers.ADD_RESULT_OF_MODEL, Modifiers.ADD_RESULT_VALIDATION_ERROR, Modifiers.ADD_RESULT_NOT_FOUND, Modifiers.ADD_SHORTCUT_BLUEPRINT_ROUTE_NOTE]
},
destroy: {
summary: 'Delete {globalId} (destroy)',
Expand Down
Loading