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

[ZEN-3764] Example Data: Generate example dummy data into OpenAPI Model from single schema (DO NOT MERGE) #487

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1470a8c
This adds an the exampledataprovider extension point that can be exte…
nbhusare Mar 28, 2019
24499d9
Passing the JSONDocument instance to all the ContentAssistExt classes
nbhusare Mar 30, 2019
01af071
Updated the getData method parameter to take a JsonNode as param
nbhusare Mar 30, 2019
ac4843a
Updated DocumentUtils to provide the URI of the document instance
nbhusare Mar 30, 2019
b69171e
Fixed the compilation issues in the unit-test
nbhusare Mar 30, 2019
6840dbb
Added an extension to the JsonReferenceProposalProvider, namely OpenA…
nbhusare Apr 2, 2019
a0b947f
Reverted back changes that were not required
nbhusare Apr 2, 2019
08cac07
Renamed ExampleDataProvider to ExampleProvider
nbhusare Apr 2, 2019
a1d3ab7
Removed the extension point and the ExampleProvider. Both have been m…
nbhusare Apr 4, 2019
3531680
Removed the extension from the plugin.xml file
nbhusare Apr 4, 2019
1167b4e
Renamed extension point to com.reprezen.swagedit.core.exampleprovider
nbhusare Apr 4, 2019
35f505d
Added base class JsonExampleProposalProvider to encapsulate common lo…
nbhusare Apr 4, 2019
e7121c9
Added generic regular expressions for example and examples
nbhusare Apr 5, 2019
be826c3
Minor method rename in the ExtensionUtils
nbhusare Apr 5, 2019
d052e3e
Merged with latest code from master
nbhusare Apr 9, 2019
0d6290b
Removed the normalization code from the Example proposal providers
nbhusare Apr 9, 2019
8d45338
ZP-3764 : dummy push to create a new build
nbhusare Apr 10, 2019
e7d4153
Removed the schema folder from the swagedit.openapi3 project and also…
nbhusare Apr 10, 2019
e719e19
Removed ExampleProposalProvider Test
nbhusare Apr 10, 2019
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 com.reprezen.swagedit.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Export-Package: com.github.fge.jsonschema.core.exceptions,
com.reprezen.swagedit.core,
com.reprezen.swagedit.core.assist,
com.reprezen.swagedit.core.assist.contexts,
com.reprezen.swagedit.core.assist.exampleprovider,
com.reprezen.swagedit.core.assist.ext,
com.reprezen.swagedit.core.editor,
com.reprezen.swagedit.core.editor.outline,
Expand Down
1 change: 1 addition & 0 deletions com.reprezen.swagedit.core/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension-point id="com.reprezen.swagedit.exampleprovider" name="com.reprezen.swagedit.exampleprovider" schema="schema/com.reprezen.swagedit.exampleprovider.exsd"/>
<extension-point id="com.reprezen.swagedit.validator" name="com.reprezen.swagedit.validator" schema="schema/com.reprezen.swagedit.validator.exsd"/>
<extension-point id="com.reprezen.swagedit.preferences" name="com.reprezen.swagedit.preferences" schema="schema/com.reprezen.swagedit.preferences.exsd"/>
<extension
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="com.reprezen.swagedit.core" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="com.reprezen.swagedit.core" id="com.reprezen.swagedit.exampleprovider" name="com.reprezen.swagedit.exampleprovider"/>
</appInfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>

<element name="extension">
<annotation>
<appInfo>
<meta.element />
</appInfo>
</annotation>
<complexType>
<sequence>
<element ref="exampleprovider"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="exampleprovider">
<complexType>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>

</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":com.reprezen.swagedit.core.assist.exampleprovider.ExampleProvider"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="apiinfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>


</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public abstract class JsonContentAssistProcessor extends TemplateCompletionProce

private final JsonProposalProvider proposalProvider;
private final JsonReferenceProposalProvider referenceProposalProvider;
private final JsonExampleProposalProvider exampleProposalProvider;
private final ContentAssistant contentAssistant;

/**
Expand All @@ -93,15 +94,19 @@ public abstract class JsonContentAssistProcessor extends TemplateCompletionProce

public JsonContentAssistProcessor(ContentAssistant ca, String fileContentType) {
this(ca, new JsonProposalProvider(),
new JsonReferenceProposalProvider(ContextType.emptyContentTypeCollection(), fileContentType));
new JsonReferenceProposalProvider(ContextType.emptyContentTypeCollection(), fileContentType),
new JsonExampleProposalProvider(ContextType.emptyContentTypeCollection()));
}

public JsonContentAssistProcessor(ContentAssistant ca, JsonProposalProvider proposalProvider,
JsonReferenceProposalProvider referenceProposalProvider,
JsonExampleProposalProvider exampleProposalProvider) {
this.contentAssistant = ca;
this.proposalProvider = proposalProvider;
this.referenceProposalProvider = referenceProposalProvider;
this.exampleProposalProvider = exampleProposalProvider;
this.textMessages = initTextMessages();
}

public JsonContentAssistProcessor(ContentAssistant ca, JsonProposalProvider proposalProvider, JsonReferenceProposalProvider referenceProposalProvider) {
this.contentAssistant = ca;
this.proposalProvider = proposalProvider;
this.referenceProposalProvider = referenceProposalProvider;
this.textMessages = initTextMessages();
}

protected abstract TemplateStore getTemplateStore();

Expand Down Expand Up @@ -145,8 +150,15 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
updateStatus();
kaizenProposals = referenceProposalProvider.getProposals(currentPath, document, currentScope);
} else {
clearStatus();
kaizenProposals = proposalProvider.getProposals(currentPath, currentModel, prefix);
final boolean isExampleCompletion = exampleProposalProvider.canProvideProposal(currentModel, currentPath);
if (isExampleCompletion) {
//TODO: Update or clear status ???????
kaizenProposals = exampleProposalProvider.getProposals(currentPath, document);
} else {
clearStatus();
kaizenProposals = proposalProvider.getProposals(currentPath, currentModel, prefix);
}

}

final Collection<ICompletionProposal> proposals = getCompletionProposals(kaizenProposals, prefix, documentOffset, selection.getText());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2019 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/
package com.reprezen.swagedit.core.assist;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import com.fasterxml.jackson.core.JsonPointer;
import com.reprezen.swagedit.core.assist.contexts.ContextType;
import com.reprezen.swagedit.core.assist.contexts.ContextTypeCollection;
import com.reprezen.swagedit.core.assist.exampleprovider.ExampleProvider;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.model.Model;
import com.reprezen.swagedit.core.utils.ExtensionUtils;

/**
* Completion proposal provider for JSON examples.
*/
public class JsonExampleProposalProvider {

public static final String ID = "com.reprezen.swagedit.exampleprovider";
protected static final String SCHEMA_FIELD_NAME = "schema";
private final ContextTypeCollection contextTypes;

public JsonExampleProposalProvider(ContextTypeCollection contextTypes) {
this.contextTypes = contextTypes;
}

public boolean canProvideProposal(Model model, JsonPointer pointer) {
return pointer != null && contextTypes.get(model, pointer) != ContextType.UNKNOWN;
}

public Collection<ProposalDescriptor> getProposals(JsonPointer pointer, JsonDocument document) {
return Collections.emptyList();
}

protected ExampleProvider getExampleDataProvider() {
final ExampleProvider defaultExampleProvider = (JsonPointer jsonPointer, JsonDocument document,
URI uri) -> "example:";

final Set<ExampleProvider> exampleProviders = ExtensionUtils.getExampleProviders();
if (exampleProviders != null && !exampleProviders.isEmpty()) {
return exampleProviders.iterator().next();
} else {
return defaultExampleProvider;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2016 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/
package com.reprezen.swagedit.core.assist.exampleprovider;

import java.net.URI;

import com.fasterxml.jackson.core.JsonPointer;
import com.reprezen.swagedit.core.editor.JsonDocument;

public interface ExampleProvider {

String getData(JsonPointer jsonPointer, JsonDocument document, URI baseURI);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static FileEditorInput getActiveEditorInput() {

return input instanceof FileEditorInput ? (FileEditorInput) input : null;
}

public static URI getActiveEditorInputURI() {
final FileEditorInput activeEditorInput = getActiveEditorInput();
return activeEditorInput != null ? activeEditorInput.getURI() : null;
}

/**
* Returns the swagger document if exists for the given path.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/*******************************************************************************
* Copyright (c) 2016 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
* Copyright (c) 2019 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/

package com.reprezen.swagedit.core.utils;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
Expand All @@ -21,45 +23,53 @@
import org.eclipse.core.runtime.Platform;

import com.reprezen.swagedit.core.Activator;
import com.reprezen.swagedit.core.assist.JsonExampleProposalProvider;
import com.reprezen.swagedit.core.assist.exampleprovider.ExampleProvider;
import com.reprezen.swagedit.core.providers.PreferenceProvider;
import com.reprezen.swagedit.core.providers.ValidationProvider;

public class ExtensionUtils {

@SuppressWarnings("unchecked")
private static <T> Set<T> resolveProviders(String ID, Predicate<? super IConfigurationElement> predicate) {
return Stream.of(Platform.getExtensionRegistry() //
.getConfigurationElementsFor(ID)) //
.filter(predicate) //
.map(e -> {
try {
return e.createExecutableExtension("class");
} catch (CoreException ex) {
Activator.getDefault().logError(ex.getMessage(), ex);
return null;
}
}) //
.map(e -> {
try {
return (T) e;
} catch (ClassCastException ex) {
return null;
}
}) //
.filter(Objects::nonNull) //
.collect(Collectors.toSet());
}
@SuppressWarnings("unchecked")
private static <T> Set<T> resolveProviders(String ID, Predicate<? super IConfigurationElement> predicate) {
return Stream.of(Platform.getExtensionRegistry() //
.getConfigurationElementsFor(ID)) //
.filter(predicate) //
.map(e -> {
try {
return e.createExecutableExtension("class");
} catch (CoreException ex) {
Activator.getDefault().logError(ex.getMessage(), ex);
return Collections.emptySet();
}
}) //
.map(e -> {
try {
return (T) e;
} catch (ClassCastException ex) {
Activator.getDefault().logError(ex.getMessage(), ex);
return null;
}
}) //
.filter(Objects::nonNull) //
.collect(Collectors.toSet());
}

public static Set<ValidationProvider> getValidationProviders() {
return resolveProviders(ValidationProvider.ID, (e) -> true);
}

public static Set<PreferenceProvider> getPreferenceProviders() {
return resolveProviders(PreferenceProvider.ID, (e) -> true);
}

public static Set<ValidationProvider> getValidationProviders() {
return resolveProviders(ValidationProvider.ID, (e) -> true);
}
public static Set<PreferenceProvider> getPreferenceProviders(String preferencePage) {
return resolveProviders(PreferenceProvider.ID,
(e) -> preferencePage.equalsIgnoreCase(e.getAttribute("preferencePage")));
}

public static Set<PreferenceProvider> getPreferenceProviders() {
return resolveProviders(PreferenceProvider.ID, (e) -> true);
}
public static Set<ExampleProvider> getExampleProviders() {
return resolveProviders(JsonExampleProposalProvider.ID, (e) -> true);
}

public static Set<PreferenceProvider> getPreferenceProviders(String preferencePage) {
return resolveProviders(PreferenceProvider.ID,
(e) -> preferencePage.equalsIgnoreCase(e.getAttribute("preferencePage")));
}
}
Loading