Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !3183

[core] Initial implementation of a validation framework in core

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Jim Schubert requested to merge github/fork/jimschubert/validator-core into master Jun 20, 2019
  • Overview 0
  • Commits 5
  • Pipelines 0
  • Changes 14

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

This adds a simple validation framework to the core module, which aims to give us a consistent way of applying errors/warnings. I'm opening this to generate some discussion about the usability of it.

I intend to use this in #2946 to pull validations out of CodegenConfigurator into a self-contained and testable type. I also intend to look into applying this to #1086. I could also see this being useful to validate certain conditions we assume to be required for passing data context to templates (see #837).

The goal here is to create a generic validator which can be applied to an OpenAPI Document, allowing us to provide warnings and errors specific to openapi-generator (in addition to those provided by swagger-parser). This has been made generic so, for example, we could apply validations to other spec documents if/when we support them. We could also create an interface which allows a generator to provide conventional typed validations for that generator.

For example:

package org.openapitools.codegen;

import org.openapitools.codegen.validator.Validator;

public interface ValidatableGenerator<T extends CodegenConfig> {
    Validator<T> getValidator();
}

This could be applied to a generator so we can provide errors and warnings prior to generation. Applying this to a generator might look like this:

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java
index 85d6f7a60d..dcaa4aff1e 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java
@@ -23,6 +23,9 @@ import io.swagger.v3.oas.models.media.Schema;
 import io.swagger.v3.parser.util.SchemaTypeUtil;
 import org.openapitools.codegen.*;
 import org.openapitools.codegen.utils.URLPathUtils;
+import org.openapitools.codegen.validator.GenericValidator;
+import org.openapitools.codegen.validator.ValidationRule;
+import org.openapitools.codegen.validator.Validator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -34,7 +37,7 @@ import java.util.Map;
 
 import static java.util.UUID.randomUUID;
 
-public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
+public class AspNetCoreServerCodegen extends AbstractCSharpCodegen implements ValidatableGenerator<AspNetCoreServerCodegen> {
 
     public static final String USE_SWASHBUCKLE = "useSwashbuckle";
     public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion";
@@ -52,6 +55,8 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
     public static final String COMPATIBILITY_VERSION = "compatibilityVersion";
     public static final String IS_LIBRARY = "isLibrary";
 
+    private GenericValidator<AspNetCoreServerCodegen> validator;
+
     private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}";
 
     @SuppressWarnings("hiding")
@@ -91,6 +96,15 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
 
         cliOptions.clear();
 
+        this.validator = new GenericValidator<>(Arrays.asList(
+            ValidationRule.warn("library + modelClassModifier", "Option 'modelClassModifier' is not supported for library outputs.",
+                    (AspNetCoreServerCodegen i)  -> "library".equals(i.library) && ("" + i.modelClassModifier.getOptValue()).length() > 0
+            ),
+            ValidationRule.warn("abstract classes/methods", "Methods must be abstract when a class is marked abstract",
+                    (AspNetCoreServerCodegen i) -> "abstract".equals(classModifier.getOptValue()) && operationModifier.getOptValue().length() > 0 && !"abstract".equals(operationModifier.getOptValue())
+            )
+        ));
+
         typeMapping.put("boolean", "bool");
         typeMapping.put("integer", "int");
         typeMapping.put("float", "float");
@@ -228,6 +242,11 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
         return "Generates an ASP.NET Core Web API server.";
     }
 
+    @Override
+    public Validator<AspNetCoreServerCodegen> getValidator() {
+        return this.validator;
+    }
+
     @Override
     public void preprocessOpenAPI(OpenAPI openAPI) {
         super.preprocessOpenAPI(openAPI);

With the above, we could allow options to treat warnings as errors and fail before writing any files.

cc @OpenAPITools/generator-core-team @OpenAPITools/openapi-generator-collaborators

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/jimschubert/validator-core