Skip to content

Commit

Permalink
#227 - add dynamic code templates for named schema properties + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tfesenko committed Oct 2, 2017
1 parent fd8c537 commit 2ee8900
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public int getLevel() {
public int getLine() {
return line;
}

@Override
public java.lang.String toString() {
return getMessage();
}

String getIndentedMessage() {
final StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ components:
type: integer
format: int32
minimum: 0

SchemaWithProperties:
type: object
required:
- name
properties:
name:
type: string
#KZOE-template name="named schema in a property", value="properties"

StringToModelMappingObject:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ValidationHelper {
val JsonNode schemaAsJson = getSchema().asJson()
val ErrorProcessor processor = new ErrorProcessor(null, null) {
override protected Set<SwaggerError> fromNode(JsonNode error, int indent) {
fail('''JSON Schema validation error: «error.asText(''')
fail('''JSON Schema validation error: «super.fromNode(error, indent''')
return super.fromNode(error, indent)
}
}
Expand Down
9 changes: 8 additions & 1 deletion com.reprezen.swagedit.openapi3/resources/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ properties:
properties:
${name}:
type: string</template>

<template name="$ref property"
id="com.reprezen.swagedit.openapi3.templates.properties.ref"
description="Property with a schema $ref template"
context="com.reprezen.swagedit.openapi3.templates.properties"
enabled="true">propertyName:
$$ref: "#/components/schemas/${schema}"</template>

<!--
CALLBACKS
-->
Expand Down Expand Up @@ -378,5 +386,4 @@ properties:
'text/html':
schema:
$$ref: '${errorModel}'</template>

</templates>
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,39 @@ public TemplateStore getTemplateStore() {
} catch (IOException e) {
YEditLog.logException(e);
}
addNamedSchemaTemplates();
addNamedSchemaTemplatesInSchemas();
addNamedSchemaTemplatesInSchemaProperties();
}
return templateStore;
}

private void addNamedSchemaTemplates() {
Template[] schemaTemplates = templateStore.getTemplates("com.reprezen.swagedit.openapi3.templates.schema");
private void addNamedSchemaTemplatesInSchemas() {
addNamedTemplates("com.reprezen.swagedit.openapi3.templates.schema",
"com.reprezen.swagedit.openapi3.templates.schemas", "schema");
}

private void addNamedSchemaTemplatesInSchemaProperties() {
addNamedTemplates("com.reprezen.swagedit.openapi3.templates.schema",
"com.reprezen.swagedit.openapi3.templates.properties", "property");
}

private void addNamedTemplates(String inlineContextId, String namedContextId, String key) {
Template[] schemaTemplates = templateStore.getTemplates(inlineContextId);
for (int i = 0; i < schemaTemplates.length; i++) {
Template schemaTemplate = schemaTemplates[i];
Template template = createNamedTemplate(schemaTemplate, "com.reprezen.swagedit.openapi3.templates.schemas");
Template template = createNamedTemplate(schemaTemplate, namedContextId, key);
templateStore.add(new TemplatePersistenceData(template, true));
}
}

private Template createNamedTemplate(Template inlineTemplate, String newTemplateId) {
private Template createNamedTemplate(Template inlineTemplate, String newTemplateId, String key) {
String indent = Strings.repeat(" ", getTabWidth());
String newPattern = inlineTemplate.getPattern().replaceAll("\n", "\n" + indent);
String pattern = String.format("${%s_name}:\n%s%s", key, indent, newPattern);
Template template = new Template(inlineTemplate.getName(), //
inlineTemplate.getDescription(), //
newTemplateId, //
"${schemaName}:\n" + indent + newPattern, //
pattern, //
inlineTemplate.isAutoInsertable());
return template;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ private SchemaBasedTemplateContextType createOpenApi3TemplateContextType(String
createOpenApi3TemplateContextType("securityScheme", "/definitions/securityScheme"),
createOpenApi3TemplateContextType("header", "/definitions/header"),
// Other
createOpenApi3TemplateContextType("mediaTypes", "/definitions/mediaTypes"));
createOpenApi3TemplateContextType("mediaTypes", "/definitions/mediaTypes"),
createOpenApi3TemplateContextType("properties", "/definitions/schema/properties/type",
"/definitions/schema/properties/properties")//
);

public List<TemplateContextType> allContextTypes() {
return allContextTypes;
Expand Down

0 comments on commit 2ee8900

Please sign in to comment.