public static boolean existSchemalessBody(Map<String, MimeType> bodies) { for (final MimeType mimeType : bodies.values()) { if (mimeType.getSchema() == null) { return true; } } return false; }
@Override public InternalEndpointRoute exampleRequest(RestModel model) { HashMap<String, MimeType> bodyMap = new HashMap<>(); MimeType mimeType = new MimeType(); String json = model.toJson(); mimeType.setExample(json); mimeType.setSchema(JsonUtil.getJsonSchema(model.getClass())); bodyMap.put("application/json", mimeType); this.exampleRequestMap = bodyMap; this.exampleRequestClass = model.getClass(); return this; }
@Override public InternalEndpointRoute exampleRequest(String bodyText) { HashMap<String, MimeType> bodyMap = new HashMap<>(); MimeType mimeType = new MimeType(); mimeType.setExample(bodyText); bodyMap.put("text/plain", mimeType); this.exampleRequestMap = bodyMap; return this; }
@Override public InternalEndpointRoute exampleRequest(Map<String, List<FormParameter>> parameters) { HashMap<String, MimeType> bodyMap = new HashMap<>(); MimeType mimeType = new MimeType(); mimeType.setFormParameters(parameters); bodyMap.put("multipart/form-data", mimeType); this.exampleRequestMap = bodyMap; return this; }
private static void fillInputRepresentation(ObjectMapper m, Map<String, Map<String, Object>> representationSamples, Action action, Operation operation, MimeType ramlInRepresentation) { ramlInRepresentation.setType(operation.getInputPayLoad().getType()); if (RamlUtils.isPrimitiveType(operation.getInputPayLoad().getType())) { Property inRepresentationPrimitive = new Property(); .generatePrimitiveSchema(inRepresentationPrimitive); try { ramlInRepresentation.setSchema(m .writeValueAsString(inRepresentationSchema)); } catch (JsonProcessingException e) { ramlInRepresentation.setSchema(operation.getInputPayLoad() .getType()); MimeType ramlInRepresentationWithMediaType; for (String mediaType : operation.getConsumes()) { ramlInRepresentationWithMediaType = new MimeType(); ramlInRepresentationWithMediaType.setSchema(ramlInRepresentation .getSchema()); try { ramlInRepresentationWithMediaType .setExample(getExampleFromPayLoad( operation.getInputPayLoad(), representationSamples, mediaType));
final MimeType mime = new MimeType(); mime.setExample(param.getExample()); if (param.getType() instanceof SchemaNameAware) mime.setSchema(((SchemaNameAware) param.getType()).getSchemaName()); mime.setFormParameters(formParamsForRaml.build());
public void exampleSchema(MimeType mimeType) { if (has(Validation.EXAMPLE)) { final String schema = mimeType.getSchema(); final String example = mimeType.getExample(); final SchemaValidator validator = findSchemaValidator(schemaValidators, MediaType.valueOf(mimeType.getType())); if (schema != null && example != null && validator != null) { validator.validate(new NamedReader(example, new Message("example").toString()), resolveSchema(raml, schema), violations, new Message("schema.example.mismatch", locator, example)); } } }
private void loadSchema(MimeType mime) { final String schema = mime.getSchema(); if (schema == null) { return; } final String refSchema = raml.getConsolidatedSchemas().get(schema); final String schemaToUse = refSchema == null ? schema : refSchema; final String type = mime.getType(); final int pos = type.indexOf(';'); final String simpleType = pos < 0 ? type : type.substring(0, pos); if (simpleType.equals("application/json") || simpleType.endsWith("+json")) { try { loadJsonSchema(loc, schemaToUse); } catch (IOException e) { throw new RuntimeException(e); } } }
private void checkFormParameters(Action action, Values values, MimeType mimeType) { if (mimeType.getSchema() != null) { requestViolations.add("schema.superfluous", locator); } @SuppressWarnings("unchecked") final Map<String, List<? extends AbstractParam>> formParameters = (Map) mimeType.getFormParameters(); if (formParameters == null || formParameters.isEmpty()) { requestViolations.add("formParameters.missing", locator); } else { checkFormParametersValues(action, mimeType, values, formParameters); } }
@Override public Parameter transform(final Context context, final MimeType source, final TransformationContext ctx) { if (StringUtils.isNotBlank(source.getSchema())) { final Dto argumentType; final JType type = ctx.getStore().getRamlMappingForSchema(source.getSchema()); if (type instanceof JDefinedClass) { argumentType = singleDtoTransformer.transform(context, (JDefinedClass) type, ctx.getNamespace()); } else if (type instanceof JClass) { argumentType = arrayDtoTransformer.transform(context, (JClass) type, ctx.getNamespace()); } else { argumentType = null; } return Parameter.builder()// .name("The schema " + source.getSchema())// .parameterType(ParameterType.BODY)// .qualifier(source.getSchema())// .example(source.getExample())// .type(argumentType)// .build(); } return null; }
@Override public Collection<com.sap.cloud.yaas.servicegenerator.model.Response> transform(final Context context, final String responseCode, final Response response, final TransformationContext ctx) { final List<com.sap.cloud.yaas.servicegenerator.model.Response> result = new ArrayList<>(); if (response.getBody() != null) { for (final Map.Entry<String, MimeType> bodyEntry : response.getBody().entrySet()) { final com.sap.cloud.yaas.servicegenerator.model.Response target = com.sap.cloud.yaas.servicegenerator.model.Response .builder()// .code(responseCode)// .description(response.getDescription())// .bodyType(bodyEntry.getValue().getType())// .example(bodyEntry.getValue().getExample())// .build(); // headers // type result.add(target); } } return result; } }
@Override public void apply(MimeType mimeType, ResourceClassBuilder resourceClassBuilder) { if (mimeType.getCompiledSchema() == null) { return; LOG.info("Process {}", mimeType.toString()); ResponseCodegenConfig responseCodegenConfig = ResponseCodegenConfig.config() .withOutputPath(resourceClassBuilder.getCodegenConfig().getOutputPath()); case XML: File xsd = File.createTempFile("schema", "xsd"); FileUtils.write(xsd, mimeType.getSchema()); respClass = new XmlCodegen(responseCodegenConfig .withPackageName(resourceClassBuilder.getCodegenConfig().getBaseObjectsPackage() responseCodegenConfig .withPackageName(resourceClassBuilder.getCodegenConfig().getBaseObjectsPackage() + addedObjectPackage(mimeType.getCompiledSchema().toString())) .withSchemaPath(mimeType.getCompiledSchema().toString()) .withInputPath(resourceClassBuilder.getCodegenConfig().getInputPath().getParent()) ).generate(); resourceClassBuilder.getResponseParser().addParser(respClass, byMimeType(mimeType), resourceClassBuilder.getCodegenConfig().getBaseObjectsPackage() + addedObjectPackage(mimeType.getCompiledSchema().toString()) );
private void setSchemaAlias(final Map<String, MimeType> body, final String schemaName) { body.get(MediaType.JSON.getCode()).setSchema(schemaName); }
@Override public void apply(MimeType body, ResourceClassBuilder resourceClassBuilder) { switch (byMimeType(body)) { case FORM: body.getFormParameters().forEach(resourceClassBuilder.applyFormParamsRule); break; case JSON: resourceClassBuilder.getApiClass().withMethod( bodyMethod() .withSchema(body.getCompiledSchema()) .withExample(body.getExample()) .withReqName(resourceClassBuilder.getReq().name()) .withInputPathForJsonGen(resourceClassBuilder.getCodegenConfig().getInputPath().getParent()) .withOutputPathForJsonGen(resourceClassBuilder.getCodegenConfig().getOutputPath()) .withPackageForJsonGen(resourceClassBuilder.getCodegenConfig().getBaseObjectsPackage() + addedObjectPackage(body.getCompiledSchema().toString())) .returns(resourceClassBuilder.getApiClass().name())); break; } }
private boolean hasAMultiTypeFormParameter(final MimeType bodyMimeType) { for (final List<FormParameter> formParameters : bodyMimeType .getFormParameters().values()) { if (formParameters.size() > 1) { return true; } } return false; }
private void addPlainBodyArgument(final MimeType bodyMimeType, final JMethod method, final JDocComment javadoc) throws IOException { method.param(types.getRequestEntityClass(bodyMimeType), GENERIC_PAYLOAD_ARGUMENT_NAME); javadoc.addParam(GENERIC_PAYLOAD_ARGUMENT_NAME).add( getPrefixedExampleOrBlank(bodyMimeType.getExample())); }
private static void fillInputRepresentation(ObjectMapper m, Map<String, Map<String, Object>> representationSamples, Action action, Operation operation, MimeType ramlInRepresentation) { ramlInRepresentation.setType(operation.getInputPayLoad().getType()); if (RamlUtils.isPrimitiveType(operation.getInputPayLoad().getType())) { Property inRepresentationPrimitive = new Property(); .generatePrimitiveSchema(inRepresentationPrimitive); try { ramlInRepresentation.setSchema(m .writeValueAsString(inRepresentationSchema)); } catch (JsonProcessingException e) { ramlInRepresentation.setSchema(operation.getInputPayLoad() .getType()); MimeType ramlInRepresentationWithMediaType; for (String mediaType : operation.getConsumes()) { ramlInRepresentationWithMediaType = new MimeType(); ramlInRepresentationWithMediaType.setSchema(ramlInRepresentation .getSchema()); try { ramlInRepresentationWithMediaType .setExample(getExampleFromPayLoad( operation.getInputPayLoad(), representationSamples, mediaType));
@Override public InternalEndpointRoute exampleRequest(JSONObject jsonObject) { HashMap<String, MimeType> bodyMap = new HashMap<>(); MimeType mimeType = new MimeType(); String json = jsonObject.toString(); mimeType.setExample(json); bodyMap.put("application/json", mimeType); this.exampleRequestMap = bodyMap; return this; }