Skip to content

Commit

Permalink
sanitize beanValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-mfg committed Oct 8, 2024
1 parent 71b5ae5 commit 2c3b38a
Show file tree
Hide file tree
Showing 50 changed files with 5,377 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/samples-java-client-jdk11.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- samples/openapi3/client/petstore/java/jersey2-java8-swagger2/**
- samples/openapi3/client/petstore/java/native**
- samples/client/others/java/okhttp-gson-oneOf/**
- samples/client/others/java/okhttp-gson-oneOf-array/**
- samples/client/others/java/resttemplate-useAbstractionForFiles/**
- samples/client/others/java/webclient-useAbstractionForFiles/**
- samples/client/others/java/jersey2-oneOf-duplicates/**
Expand All @@ -26,6 +27,7 @@ on:
- samples/openapi3/client/petstore/java/jersey2-java8-swagger2/**
- samples/openapi3/client/petstore/java/native**
- samples/client/others/java/okhttp-gson-oneOf/**
- samples/client/others/java/okhttp-gson-oneOf-array/**
- samples/client/others/java/resttemplate-useAbstractionForFiles/**
- samples/client/others/java/webclient-useAbstractionForFiles/**
- samples/client/others/java/jersey2-oneOf-duplicates/**
Expand Down Expand Up @@ -75,6 +77,7 @@ jobs:
- samples/client/petstore/java/resttemplate-swagger2/
- samples/openapi3/client/petstore/java/jersey2-java8-swagger2/
- samples/client/others/java/okhttp-gson-oneOf/
- samples/client/others/java/okhttp-gson-oneOf-array/
- samples/client/echo_api/java/okhttp-gson-user-defined-templates/
- samples/client/others/java/resttemplate-useAbstractionForFiles/
- samples/client/others/java/webclient-useAbstractionForFiles/
Expand Down
8 changes: 8 additions & 0 deletions bin/configs/java-okhttp-gson-oneOf-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: java
outputDir: samples/client/others/java/okhttp-gson-oneOf-array
library: okhttp-gson
inputSpec: modules/openapi-generator/src/test/resources/3_0/oneOf_array.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
hideGenerationTimestamp: "true"
useBeanValidation: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public void processOpts() {
convertPropertyToBooleanAndWriteBack(CONTAINER_DEFAULT_TO_NULL, this::setContainerDefaultToNull);

additionalProperties.put("sanitizeGeneric", (Mustache.Lambda) (fragment, writer) -> {
String content = fragment.execute();
String content = removeAnnotations(fragment.execute());
for (final String s: List.of("<", ">", ",", " ")) {
content = content.replace(s, "");
}
Expand Down Expand Up @@ -1793,16 +1793,20 @@ public void postProcessResponseWithProperty(CodegenResponse response, CodegenPro
return;
}

// the response data types should not contains a bean validation annotation.
// the response data types should not contain a bean validation annotation.
if (property.dataType.contains("@")) {
property.dataType = property.dataType.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
property.dataType = removeAnnotations(property.dataType);
}
// the response data types should not contains a bean validation annotation.
// the response data types should not contain a bean validation annotation.
if (response.dataType.contains("@")) {
response.dataType = response.dataType.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
response.dataType = removeAnnotations(response.dataType);
}
}

private String removeAnnotations(String type) {
return type.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
}

@Override
public ModelsMap postProcessModels(ModelsMap objs) {
// recursively add import for mapping one type to multiple imports
Expand Down
24 changes: 24 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/oneOf_array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.3
info:
title: oneOf with array inside
version: 1.0.0
paths:
/myExample:
get:
responses:
'200':
description: Response
content:
application/json:
schema:
oneOf:
- type: array
items:
"$ref": "#/components/schemas/OneOf1"
components:
schemas:
OneOf1:
type: object
properties:
message1:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
#
# This file is auto-generated by OpenAPI Generator (https://openapi-generator.tech)

name: Java CI with Maven

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
build:
name: Build oneOf with array inside
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 17, 21 ]
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --no-transfer-progress --file pom.xml
21 changes: 21 additions & 0 deletions samples/client/others/java/okhttp-gson-oneOf-array/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# exclude jar for gradle wrapper
!gradle/wrapper/*.jar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# build files
**/target
target
.gradle
build
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.github/workflows/maven.yml
.gitignore
.travis.yml
README.md
api/openapi.yaml
build.gradle
build.sbt
docs/DefaultApi.md
docs/MyExampleGet200Response.md
docs/OneOf1.md
git_push.sh
gradle.properties
gradle/wrapper/gradle-wrapper.jar
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
pom.xml
settings.gradle
src/main/AndroidManifest.xml
src/main/java/org/openapitools/client/ApiCallback.java
src/main/java/org/openapitools/client/ApiClient.java
src/main/java/org/openapitools/client/ApiException.java
src/main/java/org/openapitools/client/ApiResponse.java
src/main/java/org/openapitools/client/Configuration.java
src/main/java/org/openapitools/client/GzipRequestInterceptor.java
src/main/java/org/openapitools/client/JSON.java
src/main/java/org/openapitools/client/Pair.java
src/main/java/org/openapitools/client/ProgressRequestBody.java
src/main/java/org/openapitools/client/ProgressResponseBody.java
src/main/java/org/openapitools/client/ServerConfiguration.java
src/main/java/org/openapitools/client/ServerVariable.java
src/main/java/org/openapitools/client/StringUtil.java
src/main/java/org/openapitools/client/api/DefaultApi.java
src/main/java/org/openapitools/client/auth/ApiKeyAuth.java
src/main/java/org/openapitools/client/auth/Authentication.java
src/main/java/org/openapitools/client/auth/HttpBasicAuth.java
src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java
src/main/java/org/openapitools/client/model/MyExampleGet200Response.java
src/main/java/org/openapitools/client/model/OneOf1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.9.0-SNAPSHOT
22 changes: 22 additions & 0 deletions samples/client/others/java/okhttp-gson-oneOf-array/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Generated by OpenAPI Generator: https://openapi-generator.tech
#
# Ref: https://docs.travis-ci.com/user/languages/java/
#
language: java
jdk:
- openjdk12
- openjdk11
- openjdk10
- openjdk9
- openjdk8
before_install:
# ensure gradlew has proper permission
- chmod a+x ./gradlew
script:
# test using maven
#- mvn test
# test using gradle
- gradle test
# test using sbt
# - sbt test
138 changes: 138 additions & 0 deletions samples/client/others/java/okhttp-gson-oneOf-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# openapi-java-client

oneOf with array inside
- API version: 1.0.0
- Generator version: 7.9.0-SNAPSHOT

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)


*Automatically generated by the [OpenAPI Generator](https://openapi-generator.tech)*


## Requirements

Building the API client library requires:
1. Java 1.8+
2. Maven (3.8.3+)/Gradle (7.2+)

## Installation

To install the API client library to your local Maven repository, simply execute:

```shell
mvn clean install
```

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

```shell
mvn clean deploy
```

Refer to the [OSSRH Guide](http://central.sonatype.org/pages/ossrh-guide.html) for more information.

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-java-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
```

### Gradle users

Add this dependency to your project's build file:

```groovy
repositories {
mavenCentral() // Needed if the 'openapi-java-client' jar has been published to maven central.
mavenLocal() // Needed if the 'openapi-java-client' jar has been published to the local maven repo.
}
dependencies {
implementation "org.openapitools:openapi-java-client:1.0.0"
}
```

### Others

At first generate the JAR by executing:

```shell
mvn clean package
```

Then manually install the following JARs:

* `target/openapi-java-client-1.0.0.jar`
* `target/lib/*.jar`

## Getting Started

Please follow the [installation](#installation) instruction and execute the following Java code:

```java

// Import classes:
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.Configuration;
import org.openapitools.client.model.*;
import org.openapitools.client.api.DefaultApi;

public class Example {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost");

DefaultApi apiInstance = new DefaultApi(defaultClient);
try {
MyExampleGet200Response result = apiInstance.myExampleGet();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling DefaultApi#myExampleGet");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}

```

## Documentation for API Endpoints

All URIs are relative to *http://localhost*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*DefaultApi* | [**myExampleGet**](docs/DefaultApi.md#myExampleGet) | **GET** /myExample |


## Documentation for Models

- [MyExampleGet200Response](docs/MyExampleGet200Response.md)
- [OneOf1](docs/OneOf1.md)


<a id="documentation-for-authorization"></a>
## Documentation for Authorization

Endpoints do not require authorization.


## Recommendation

It's recommended to create an instance of `ApiClient` per thread in a multithreaded environment to avoid any potential issues.

## Author



Loading

0 comments on commit 2c3b38a

Please sign in to comment.