Swagger codegen generate too long string literals - java

I need to generate classes based on provided swagger file. Unfortunately I have no influence on content of this file.
My config in gladle looks like:
task generateApi {
doLast {
def config = new CodegenConfigurator()
config.setInputSpec("file:///$projectDir/$swaggerSourceFile")
config.setOutputDir("$projectDir/$swaggerTargetFolder")
config.setLang('spring')
config.setAdditionalProperties([
'interfaceOnly' : true,
'apiPackage' : 'somepackage',
'modelPackage' : 'somepackage.domain',
'sourceFolder' : '',
'java8' : true,
'dateLibrary' : 'java8',
'skipDefaultInterface': true
])
new DefaultGenerator().opts(config.toClientOptInput()).generate()
}
}
Exception which I`m getting is : "error: constant string too long"
Problem is that even I set 'skipDefaultInterface', swagger will generates it. In default interface it generates default response entity body which is json file which is 70k char long string.
Please help!

Looks like this is a known issue with the spring generator in Swagger Codegen:
https://github.com/swagger-api/swagger-codegen/issues/9055
A PR for this issue is available but not yet merged (as of February 2020):
https://github.com/swagger-api/swagger-codegen/pull/8691
Try using openapi-generator v. 4.2.2 or later which is supposed to include a similar fix.

Related

org.json breaking change with update

I have a legacy system at hand. This system was using an old version of org.json.JSONObject. For this version the following json was valid
{
"key" = "value",
"key2": "value2"
}
The json is actually invalid but for this version it was valid.
Now I upgraded the version to the latest one and everything is failing because of =. I get error that Expected ':' at char XX everytime. I could change my local tests and integ tests but what about clients? I have no idea whether they pass = or : and knowing them they would be passing =.
Is there a way i make the latest version on JSONObject accept = as valid assignment and alternative to : or is this a lost cause?

How to remove ApiUtil.java from openApi geneate task with openapi-generator-gradle-plugin:4.3.0

I have am using openapi-generator-gradle-plugin:4.3.0 to generate the api and models from a openApi-generate.yaml file. I have set skipDefaultInterface: "true" in configOptions, the default implementation of interfaces is not generated and ApiUtil.java is not used anywhere. (The default implementations used ApiUtil.java)
What I want is to remove (disable the generation of) ApiUtil.java from generated_sources, as it is not used in code, and its default code is creating security-issues in pipeline as well.
What I have tried: I have tried adding different options::
supportingFilesConstrainedTo = []
supportingFiles = ""
supportingFilesToGenerate = ""
apiFilesConstrainedTo = []
But I haven't been able to remove this file from being generated. I went through this: OpenApi generation Customization, but looks like it is not applicable for the given version.
Current gradle buildscript looks as:
task generateTask(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "spring"
inputSpec = "$projectDir/src/main/resources/specs/openApi-generate.yaml"
outputDir = "$buildDir/generated-sources"
apiPackage = "com.example.openapi.api"
modelPackage = "com.example.openapi.model"
generateModelDocumentation = false
generateApiDocumentation = false
generateModelTests = false
generateApiTests = false
configOptions = [
dateLibrary: "java8",
interfaceOnly: "true",
serializableModel: "true",
skipDefaultInterface: "true"
]
}
Answering my own question. I had to do following things:
Increase Open Api version in build.gradle. This is important becuase it was not supported in older versions:
id "org.openapi.generator" version 5.3.1
Inside build script provide following options
globalProperties = [
apis : ""
]
A bit late but always good to give a good answer.
You have to use the configOption skipDefaultInterface but you also need to set the property generateSupportingFiles to false (which is NOT one of the configOptions but goes directly under configuration)
Place generateSupportingFiles: "false" under configOptions.

How to generate fields of type {"type":"string","avro.java.string":"String"} using avro-tools-1.9.1.jar IDL?

We have JAVA gradle avro plugin (davidmc24/gradle-avro-plugin) generate shema, and with default stringType string, which willl generated JAVA POJO like this:
Class A{
Shema = ...,{"type":"string","avro.java.string":"String"}...
string batchName;
}
then we use same avdl file to generate C# package, our solution is first generate avsc file with avro-tools-1.9.1.jar, which will generate avsc like this
"fields" : [ {
"name" : "batchName",
"type" : "string"
},
WE have a C# producer , and java consumer, in this case , batchName can't been deserilize in JAVA consumer side, we got below error:
org.apache.avro.util.Utf8 cannot be cast to java.lang.String
So, my guess is shema dissync, could you help point out how to let avro-tools-1.9.1.jar also generated fields with type avro.java.string keywords,so we can Thank you!
GenericDatumReader trade string and java.lang.String different code is here:
if (logicalType != null) {
Conversion<?> conversion = getData().getConversionFor(logicalType);
if (conversion != null) {
return convert(datum, expected, logicalType, conversion);
}
}
My solution will be using schema registry. And in common you will have first 1 to 5 bytes as schema ID in registry. You can desalinize the data using that schema. And get and genericRecord. I think this should solve your problem.

How to force fge json-schema validator to throw an error on unknown keywords?

I want my application to receive a Json-schema and later validate if some JSON objects comply with the schema. My problem is that, if I provide false Json-schema, I don't get an exception but just some warning in the logs. So I either want to be thrown with an exception, or somehow take the warnings into consideration. However, the warning logs seem rather silent to me.
I am using java and the library is the pretty much standard one for json-schema
String json = "{\"a\":\"b\"};
JsonNode schema = new ObjectMapper().readTree(json);
JsonSchemaFactory.byDefault().getSyntaxValidator();
// this returns true but I want it to return false, since "a" is not a valid keyword
validator.schemaIsValid(schema);
// this returns a waning --> the following keywords are unknown and will be ignored: [a]
System.out.println(validator.validateSchema(schema));
The meta-schema of JSON Schema ("the schema of schemas") permits additional properties, so the fge validator also does, and there is no reason why the implementation would change this restriction.
One thing you can do is downloading the meta-schema, altering it to "additionalProperties": false , then load this changed meta-schema using the validator and validate your own schema with it.
The draft4 meta-schema is here: http://json-schema.org/draft-04/schema
I was able to change the error threshold needed for the FGE validator to consider the validation as unsuccessful when unknown properties are provided. The following code does the work
void validateSchema(JsonNode schemaNode) throws JsonProcessingException {
SyntaxValidator validator = JsonSchemaFactory.byDefault().getSyntaxValidator();
ProcessingReport report = new ListProcessingReport(null, LogLevel.INFO);
ValueHolder<SchemaTree> holder = ValueHolder.<SchemaTree>hold("schema",
new CanonicalSchemaTree(SchemaKey.anonymousKey(), schemaNode));
Processor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> processor =
validator.getProcessor();
report = ProcessingResult.uncheckedResult(processor, report, holder).getReport();
if (!report.isSuccess()) {
IllegalArgumentException ex = new IllegalArgumentException();
for (ProcessingMessage processingMessage : report) {
ex.addSuppressed(processingMessage.asException());
}
throw ex;
}
}
Good approach #erosb, #Llvanov. As an alternative, I was able to get the parser to validate for unknown properties by injecting the 'additionalProperties' attribute directly into the schema node.
((ObjectNode) schemaNode).put(
"additionalProperties",
false);

Add extension to CertificateRequestMessage

I'm trying to put a custom element (a template name to use for the PKI) in my Certificate request that I generate using bouncycastle library.
The RFC says that you can use an extension that follows :
id-regInfo-utf8Pairs OBJECT IDENTIFIER ::= { id-regInfo 1 }
--with syntax UTF8Pairs ::= UTF8String
The few lines of code concerning this are
CertificateRequestMessageBuilder msgbuilder = new
CertificateRequestMessageBuilder(BigInteger.valueOf(reqId));
msgbuilder.addExtension(new ASN1ObjectIdentifier("1.3.6.1.5.5.7.7.5.2.1"), false, ???);
I can't find any information on what to put in that addExtension function, nor any example to adapt from.
I guess the new ASN1ObjectIdentifier("1.3.6.1.5.5.7.7.5.2.1") is close to what is expected, but the following ASN1Encodable that is supposed to be the value of that field is a mystery to me.
I just want to get something along template:encipherment in that field.
This seems like a pretty simple thing to do but the lack of documentation coupled to my lack of experience with BouncyCastle causes me a lot of problems.
The questions are :
Am I doing what I want the correct way ? (Using the extensions of the CRMF to convey my custom field)
If I do, how do I make this work ?

Categories